[eluser]Alex Kendrick[/eluser]
Hi,
I'm working on my first CI project and enjoying the framework. I am also still somewhat new to PHP so it is possible I am missing something fundamental that is keeping me from being able to stick to MVC pattern. Here is what I am trying to do:
Query database for project information (name, id, etc), render it in the view (this I can do) BUT THEN, for each result, run a query based off one of the fields returned in the result (project id).
Here is my code. It works, but I have to put the second query in the view. There must be a way to have this all in the controller, but I'm stuck.
The Controller:
Code:
$data['query']= $this->db->query('SELECT gallery_text, gallery_id, proj_name FROM galleries, projects WHERE section_id=5 AND galleries.proj_id = projects.proj_id');
The View:
Code:
<?php foreach($query->result() as $row): ?>
<p><?=$row->proj_name?></p>
<p><?=$row->gallery_text?></p>
<?php // nested query for images based off each of the first query result gallery_id
$img_query= $this->db->query("SELECT src FROM images WHERE gallery_id = $row->gallery_id");
foreach($img_query->result() as $row){
echo $row->src .'<br />';
}
?>
<hr/>
<?php endforeach; ?>
Can anyone point me in the right direction on how to accomplish the second query (the one nested in the loop in the view) inside the controller?
Thank you for any advice!