Running a Query with in a foreach loop - El Forum - 08-15-2009
[eluser]jshultz[/eluser]
So, after all your help I thought you would like to know how I finally ended up solving this problem. Your advice was really helpful and has encouraged me to not give up and I'm really grateful.
First, here's the model:
Code: function tasksByProjectId() {
/* $sql = "SELECT projectid, project_name, taskid, task\n"
. "FROM projects\n"
. "LEFT JOIN tasks ON tasks.project_id = projects.projectid LIMIT 0, 30 "; ";
$query - $this->db->query($sql);
return $query->result_array(); */
$this->db->select('*');
$this->db->from('projects');
$this->db->join('tasks', 'tasks.project_id = projects.projectid', 'left');
$query = $this->db->get();
return $query->result();
}
Here's the control:
Code: function project_list()
{
if (!$this->tank_auth->is_logged_in()) {
redirect('/auth/login/');
} else {
$this->load->model('Project_model');
$data['projectList'] = $this->Project_model->selectFromProjects();
$data['user_id'] = $this->tank_auth->get_user_id();
$data['username'] = $this->tank_auth->get_username();
$data['projects'] = $this->Project_model->getByUserId();
$data['tasks'] = $this->Project_model->getTasks();
$data['projectPlusTasks'] = $this->Project_model->tasksByProjectId();
$data['page_title'] = 'OSM Projects';
$data['page'] = 'project-list-view'; // pass the actual view to use as a parameter
$this->load->view('container',$data);
}
}
and finally here's the view:
Code: <table cellpadding="0" cellspacing="0" class="stripeMe">
<tr><th>Project ID</th><th>Project Name</th><th>Task ID</th><th>Task Name</th></tr>
<?php foreach($projectPlusTasks as $row): ?>
<tr>
<td><strong>Project:</strong> <?=$row->projectid?></td>
<td><?=$row->project_name?></td>
<td><?=$row->p_due_date?></td>
<td></td>
</tr>
<tr>
<td><strong>Task:</strong><?=$row->taskid?></td>
<td><?=$row->task?></td>
<td><?=$row->t_due_date?></td>
<td></td>
</tr>
<?php endforeach; ?>
</table>
I even tried the view in a different way and it worked:
Code: <table class="stripeMe">
<?php
$projectid = -1;
foreach ($projectPlusTasks as $row) {
if ($row->$projectid <> $projectid) {
echo '<tr><td><strong>Project:</strong>' . $row->project_name . '</td></tr>';
$projectid - $row->projectid;
}
echo '<tr><td><strong>Task:</strong>' . $row->taskid . ' ' . $row->task . '</td></tr>';
}
?>
</table>
So, it's worked out. It's not exactly how I wanted. For example, a blank Task line is rendered out if the project has no tasks. But, the overall goal of this question was accomplished and I feel pretty good. Thanks!
|