CodeIgniter Forums
Running a Query with in a foreach loop - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Running a Query with in a foreach loop (/showthread.php?tid=21423)

Pages: 1 2 3


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>
&lt;?php foreach($projectPlusTasks as $row): ?&gt;
    
    <tr>
        <td><strong>Project:</strong> &lt;?=$row->projectid?&gt;</td>
        <td>&lt;?=$row->project_name?&gt;</td>
        <td>&lt;?=$row->p_due_date?&gt;</td>
        <td></td>
    </tr>
    <tr>
        <td><strong>Task:</strong>&lt;?=$row->taskid?&gt;</td>
        <td>&lt;?=$row->task?&gt;</td>
        <td>&lt;?=$row->t_due_date?&gt;</td>
        <td></td>
    </tr>
    
&lt;?php endforeach; ?&gt;
</table>


I even tried the view in a different way and it worked:

Code:
<table class="stripeMe">
&lt;?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>';
    }

?&gt;

</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! Smile