views:

73

answers:

0

I'm struggling with some looping logic where I need to generate charts which plot a member's change in height against the average height for other members at the same age. The data is structured as follows:

A member can have many visits. Each visit belongs to a member (ref member_id). A visit can have more than one height entry Heights belong to a visit.

I can access a member's heights with the following (using an ORM):

$data = array();

        foreach ($this->member->visits as $v)
        {
            foreach ($v->heights as $h)
            {
                $data[] = $h->height;
            }   
        }

If a member only has 2 visits, I need to find out the member's age at the time of those visits and store the height info against the age of the member. Using only those ages (e.g 14 and 15 years), I need to work out the average height of other members when they were also at those ages. I'm having trouble getting the logic right for this, but here is my incomplete and incorrect code - hope someone can help!

$ages = array();
        //get all entries belong to current member
        foreach ($this->member->visits as $v)
        {
            //get all heights belonging to this entry
            foreach ($v->member_heights as $h)
            {
                //calculate member's age at time of this entry and add to array
                $ages[] = member::years($v->updated, $v->member->dob); 
            }   
        }

        $data = array();
        //get all 
        $visits = ORM::factory('visit')->find_all();

        //run through each of the ages which the current member has a height entry for
        foreach ($ages as $age)
        {
            //need to run through all of the data entries
            foreach ($visits as $v)
            {
                //look at entries with member heights entered
                foreach ($v->member_heights as $h)
                {

                    $others_ages[] = member::years($v->updated, $v->member->dob); 
                }   
            }
        }

EDIT: to try and expand on this and explain a bit better, I have the following function which correctly collates and outputs the member's height and the age they were at that time. I need to try and use the $ages array to generate the average height of all other members for a comparison. Hope this makes more sense!

    public function height()
            {
                $data = array();
                //build format required for highcharts output
                foreach ($this->member->visits as $v)
                {
                    foreach ($v->heights as $h)
                    {
                        $data[] = $h->height;
                                //this calculates the age at the time of the entry
                               $ages[] = member::years($v->updated, $v->member->dob); 
                    }   
                }
                echo json_encode($data);
echo json_encode($ages);
            }