Welcome Guest, Not a member yet? Register   Sign In
Looping through left joined tables
#3

[eluser]sophistry[/eluser]
hi jshultz...

this is how i've approached joined output. maybe it will help you:
Code:
// this is a very simple sub-summary function that will give you nested arrays
//  call it like this
// $res is a ci query result object
$new_array = nest_flat_records($res,'name_of_field_result_is_sorted_on','name_of_field_in_joined_table');

// take the output from a join
    // (where the records are duplicated except
    // for one data point) and nest the
    // desired data point $pluck_out
    function nest_flat_records($res,$group_by_value,$pluck_out)
    {
        $new = array();
        $sub = array();
        $prev_group_by_value = '';
        foreach($res as $k=>$r)
        {
            if ($r->$group_by_value === $prev_group_by_value)
            {
                // add a sub item
                $sub[] = $r->$pluck_out;
            }
            else
            {
                // it's first record or the next group
                if (! $k) // first
                {
                    $kp = $k;
                    $sub[] = $r->$pluck_out;
                    unset($r->$pluck_out);
                    $new[$kp] = $r;

                }
                else // subsequent
                {
                    // store the previous group's
                    // sub array, then re-initialize it
                    
                    $new[$kp]->pluck_out = $sub;
                    $kp = $k;
                    $sub = array();
                    $sub[] = $r->$pluck_out;
                    unset($r->$pluck_out);
                    $new[$k] = $r;

                }
            }
            $prev_group_by_value = $r->$group_by_value;
        }    
        // cap it with the last one
        $new[$kp]->pluck_out = $sub;
        return $new;
    }


Messages In This Thread
Looping through left joined tables - by El Forum - 10-04-2009, 01:53 PM
Looping through left joined tables - by El Forum - 10-04-2009, 09:20 PM
Looping through left joined tables - by El Forum - 10-05-2009, 08:10 AM
Looping through left joined tables - by El Forum - 10-05-2009, 08:27 AM
Looping through left joined tables - by El Forum - 10-05-2009, 09:56 AM



Theme © iAndrew 2016 - Forum software by © MyBB