Welcome Guest, Not a member yet? Register   Sign In
Running a Query with in a foreach loop

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:

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->join('tasks', 'tasks.project_id = projects.projectid', 'left');
    $query = $this->db->get();
    return $query->result();

Here's the control:

function project_list()
          if (!$this->tank_auth->is_logged_in()) {
          } else {
            $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


and finally here's the view:

<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;
        <td><strong>Project:</strong> &lt;?=$row->projectid?&gt;</td>
&lt;?php endforeach; ?&gt;

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

<table class="stripeMe">

    $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>';



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

Theme © iAndrew 2016 - Forum software by © MyBB