Welcome Guest, Not a member yet? Register   Sign In
Multiple Foreach Loops
#2

[eluser]mddd[/eluser]
It's not efficient to look up the clips for every sheet separately. Use a JOIN query to get both lists at the same time.
Code:
// get the data
$this->db->from('cue_sheets');
$this->db->where('cue_sheets.id', $id);
$this->db->join('clips', 'clips.sheet_id = cue_sheets.id', 'left');
$rawdata = $this->db->get()->result_array();
// prepare the data into a multidimensional array
$data = array();
foreach($rawdata as $row)
{
  // if this is the first clip of a new sheet, make a new entry for it
  if (!isset($data[$row['id']]))
  {
    $data[$row['id']] = $row;
    $data[$row['id']]['clips'] = array();
  }  

  // add the current clip to the sheet
  $data[$row['id']]['clips'][] = $row;
}

Now in your view you can loop through the sheets, and within that, loop through the clips:
Code:
foreach($data as $sheet) {
  // make header etc.
  if (sizeof($sheet['clips']))
  {
    foreach($sheet['clips'] as $clip)
    {
      // show clip
    }
  } else {
    // show 'no clips'
  }
}


Messages In This Thread
Multiple Foreach Loops - by El Forum - 06-15-2010, 12:02 AM
Multiple Foreach Loops - by El Forum - 06-15-2010, 12:32 AM



Theme © iAndrew 2016 - Forum software by © MyBB