Where you'd do something like this in plain old PHP:
$result = mysql_query("SELECT value FROM table");
while ($row = mysql_fetch_assoc($result)): mysql_query("SELECT id FROM table WHERE field='" . $row['value'] . "'");
endwhile;
in CodeIgniter you put the following code in your controller function:
$query = $this->Model->function($params);
$result = $query->result();
$foo = array();
$foo = array();
foreach ($result as $id => $object):
foreach ($object as $key => $value):
$foo[$id][$key] = $value;
endforeach;
$foo[$id]['bar'] = $this->function($params);
endforeach;
$foo[$id]['bar'] = $this->function($params);
endforeach;
$this->data['foo'] = $foo;
$this->load->view('baz', $this->data);
then in the view:
foreach ($foo as $key => $row):
print $row['item1'];
print $row['item2'];
print $row['bar'];
endforeach;Basically, instead of passing the result of the main query direct to the view and looping through it there, you loop through the query results in the controller instead, and build up a two-dimensional associative array of results, to which you can add additional row-specific query results. Then in the view you just loop through your custom array.
Update: You need to initialise the array ($foo = array()) before the foreach loop in the controller, otherwise if the query returns an empty result you'll get an error!
1 comment:
Yikes. Bad advice. MySQL is a relational database. Use JOINs
Post a Comment