Welcome Guest, Not a member yet? Register   Sign In
Looping through left joined tables
#1

[eluser]jshultz[/eluser]
I've got a small problem: I've got a model that's doing a left join on two tables. A projects table and a tasks table. It's supposed to echo out the project, then echo out all the tasks associated with that project. Instead,it's repeating the project name for each task row. Here's the code: http://stikked.com/view/25802955 do you think a second foreach loop would work? inside the first foreach loop?

VIEW:
Code:
<?php $currentProjectID = NULL; ?>
<?php if(count($projectPlusTasks) > 0) : foreach ($projectPlusTasks as $row): ?>
    
        <?php if ( $currentProjectID != $row->p_id ): ?>
        <tr>
            <td>
                <span class="project_name">Project: &lt;?= $row->p_name?&gt;</span>
            </td>
            <td>
                Due Date: &lt;?= $row->p_due_date?&gt;
            </td>
            <td>
                
            </td>
            <td>
            </td>
        </tr>
        &lt;?php endif;?&gt;
        <tr>
            <td>
                <strong>Task:</strong>&lt;?= $row->task?&gt;
              
            </td>
            <td>
                Due Date: &lt;?= $row->t_due_date?&gt;
            </td>
            <td>
                
            </td>
            <td>
            </td>
        </tr>
        <tr>&lt;form id="submit_Tasks" action="/site/submit_tasks" method="post"&gt;
            &lt;?php echo form_hidden('user_id', $user_id); ?&gt;
            &lt;input type="hidden" name="project_id" value="&lt;?=$row-&gt;p_id?&gt;">
            <td>New Task: &lt;input type="text" id="task" name="task" /&gt;&lt;/td>
            <td>Date: &lt;input type="text" id="&lt;?=$row-&gt;p_id?&gt;" class="datepicker" name="t_due_date" /></td>
            <td>&lt;input type="submit" name="submit" value="Submit" /&gt;&lt;/td>
            &lt;/form&gt;&lt;/tr>
        &lt;?php endforeach; ?&gt;
        
        &lt;?php else : ?&gt;
            
            <p class="notice">No Projects Found. Why don't you add one?</p>
            
          &lt;?php endif; ?&gt;
MODEL:
Code:
/* Get all Projects and Tasks by Project ID */
function tasksByProjectId() {
    
    $this->db->select('*');
    $this->db->from('projects');
    $this->db->join('tasks', 'tasks.projectid = projects.p_id', 'left');
    $query = $this->db->get();
    return $query->result();
}


Messages In This Thread
Looping through left joined tables - by El Forum - 10-04-2009, 01:53 PM
Looping through left joined tables - by El Forum - 10-04-2009, 09:20 PM
Looping through left joined tables - by El Forum - 10-05-2009, 08:10 AM
Looping through left joined tables - by El Forum - 10-05-2009, 08:27 AM
Looping through left joined tables - by El Forum - 10-05-2009, 09:56 AM



Theme © iAndrew 2016 - Forum software by © MyBB