Welcome Guest, Not a member yet? Register   Sign In
Passing data plus some related data to a view
#1

[eluser]vinofilus[/eluser]
I have a database with 3 tables: Task, Comments, People
This is a starter project as I try to learn codeigniter.

The controller calls the model which grabs and joins the data from the Task and People tables to send to the view for nice presentation. That part works fine.

The next thing I'd like to do is show the two most recent comments connected to each task. Here is where I get stumped.

I'm thinking I could make a foreach query loop in the controller which calls a "get_2_comments" function in the model and somehow populate the array I'm sending to the view, but it's just not coming together for me.

Here is the code I've been toying with so far. These parts all seem to work fine.
But as soon as I try a loop, things go awry.
I welcome your direction on how to proceed.

Controller
Code:
function index()
{
    $data['heading'] = "WDT Project Task List";
    $data['query'] = $this->Task_model->get_tasks();
    $this->load->view('tracker_view', $data);
}

Relevant part of the model
Code:
function get_tasks()
{
    $this->db->select('    Task.id,
                Task.title,
                Task.entered_date,
                Task.body,
                Task.scope,
                Task.importance,
                Task.people_id,
                Task.updated_date,
                People.name as submitter');
    $this->db->join('People', 'Task.People_id = People.id');
    $this->db->from('Task');
    $query = $this->db->get();
    return $query->result();
}

And the view
Code:
<?php foreach($query as $row): ?>
    <div class="tasktitle">&lt;?=$row->title?&gt;</div>
    <div class="taskdetails">
    <p>Opening Date: &lt;?=$row->entered_date?&gt;<br><b>&lt;?=$row->body?&gt;</b></p>
    <p>Scope: &lt;?=$row->scope?&gt;<br>Importance: &lt;?=$row->importance?&gt;<br>Submitter: &lt;?=$row->submitter?&gt;</p>
    <p>Updated Date: &lt;?=$row->updated_date?&gt;</p>
    </div>
&lt;?php endforeach; ?&gt;

Thanks!




Theme © iAndrew 2016 - Forum software by © MyBB