Welcome Guest, Not a member yet? Register   Sign In
Sorry, but I just don't get it
#1

[eluser]Bionicjoe[/eluser]
Controller does this. (It works)
Code:
$this->data = array(
            'update' => $this->outage_model->listoutageupdates($id),

Here's the happy Model.
Code:
function listoutageupdates($id)
    {
        $this->db->order_by("updateid", "desc");
        $query = $this->db->get_where('updates', array('outageid' => $id));

        if ($query->num_rows() > 0)
        {
            foreach($query->result() as $row)
            {
                echo $row->text;
                echo $row->updateid;
            }
    }
Model echoes the right thing, but I want to pass this data to the view. I only get class errors and garbage when I try to pass it.

The View
Code:
<?php
        if ($update > 0)
        {
            foreach ($update as $item->$i)
            {
                ?&gt;<hr>
                <b>Update #&lt;?php echo $item+1 ?&gt;</b><br />&lt;?php
                echo $i;
            }
        }
        ?&gt;

How do I get the results to the view?
(Some of the var names are wrong because I've been changing stuff around so much. Posted this mid-frustration.)
#2

[eluser]Clooner[/eluser]
When you load the view in your controller use
Code:
$this->load->view('theview', $this->data);

then you should be able to access the data in your view using $update
#3

[eluser]cahva[/eluser]
And addition to the last answer, you should return the result from model, not echo it. I dont know if your code was just to test things and you have returned the result but here goes:

Code:
function listoutageupdates($id)
    {
        $this->db->order_by("updateid", "desc");
        $query = $this->db->get_where('updates', array('outageid' => $id));

        return $query->result();
    }

And view should be sumthing like this(althought im not entirely sure what you want to output):
Code:
&lt;?php
        if ($update)
        {
            foreach ($update as $item)
            {
                ?&gt;<hr>
                <b>Update #&lt;?php echo $item->text.($item->updateid+1) ?&gt;</b><br />&lt;?php
                echo $item->updateid;
            }
        }
        ?&gt;
#4

[eluser]Bionicjoe[/eluser]
Thanks cahva!

I went home last night and did some reading that cleared up CI immensely.
My key mistake was thinking of MVC data flow as:
Controller -> Model -> View

It's really
Controller <-> Model
then Controller -> View

Your example worked but I just needed to move the vars around to present it properly.
Also those echos in the Model were just to see if the model was working.

One more question.
If my query returns no results how can I make it send a "No results found." message to the view.

What I'm trying now, but the 'else' part does nothing.
Code:
function listoutageupdates($id)
    {
        $this->db->order_by("updateid", "desc");
        $query = $this->db->get_where('updates', array('outageid' => $id));

        if ($query->num_rows() > 0)
        {
            return $query->result();
        }
        else
        {
            // Return a no results found message.
            return $query = 'No updates found.';
        }
    }
#5

[eluser]cahva[/eluser]
You should just return FALSE if no rows are found:
Code:
if ($query->num_rows() > 0)
        {
            return $query->result();
        }
        else
        {
            // Return a no results found message.
            return FALSE;
        }

With a result or boolean FALSE its easy to use that in the view:
Code:
&lt;?php if ($update): ?&gt;
    &lt;?php foreach ($update as $item): ?&gt;
        <hr>
        <b>Update #&lt;?php echo $item->text.($item->updateid+1) ?&gt;</b><br />
        &lt;?php echo $item->updateid;
    &lt;?php endforeach; ?&gt;
&lt;?php  else: ?&gt;
    No updates found.
&lt;?php endif; ?&gt;
#6

[eluser]Bionicjoe[/eluser]
Thanks again, cahva!




Theme © iAndrew 2016 - Forum software by © MyBB