0

So I'm playing around with CodeIgniter for a bit. Right now I'm trying to build a "page overview" page. I got another page where i can create pages, and i want to display those pages in an overview.

Right now i my model i got the following:

public function page_list(){
    $query = $this->db->get('pages');

    if ($query){
        $pageListing = "<table class='table table-striped'>";
        foreach ($query->result() as $row){
            $pageListing .= "<tr>";
            $pageListing .= "<td>".$row->title."</td>";
            $pageListing .= "<td>".$row->category."</td>";
            $pageListing .= "<td>".$row->date."</td>";
            $pageListing .= "<td><a href='delete_page/".$row->id."'>delete</a></td>";
            $pageListing .= "<td><a href='edit_page/".$row->id."'>edit</a></td>";
            $pageListing .= "</tr>";
        }
        $pageListing .= "</table>";
    } else {

        $pageListing = "There are no pages to display";
    }
}

I know it's not the best way of doing it. I'm obviously writing html code that belongs in the view in a model. Right now i don't bother about that. I'm just trying to figure out how to use the variable $pageListing in my controller, and from there load in into a view.

I tried using the variable in the following way:

public function displayPages {
    $this->load->model('model_pages');
    $this->model_pages->page_list();
    echo $this->model_pages->pageListing;
}

When i can do this successfully i can use $pageListing in my view.

Right now i get the following error:

A PHP Error was encountered
Severity: Notice
Message: Undefined property: Main::$pageListing
Filename: core/Model.php
Line Number: 51

As i said before, i know this is not the best practice, to code html in the model, and echo it from the controller. This is pure for experimenting, and I'm just curious how i can make this work. Since i will have to work with other variable from the model in the future, and that will work the exact sameway, except they won't contain whole html codes.

1
  • 1
    Another way to think about this -- in your model if there are no results - return false. then in your controller if $this->model_pages->page_list(); returns false -> show a no results view page. That way the "controller" is "controlling" what view is being shown. Commented Nov 25, 2013 at 21:04

1 Answer 1

2

Return the $pageListing from the model back to the controller. Add this line at the end of your model function:

return $pageListing;

Edit:

public function displayPages {
    $this->load->model('model_pages');
    echo $this->model_pages->page_list();
    //echo $this->model_pages->pageListing;
}

OR

public function displayPages {
    $this->load->model('model_pages');
    $data['pageList'] = $this->model_pages->page_list(); //assign to a variable
    $this->load->view('pagelist', $data);  //is a file pagelist.php in your views folder

}

Now if you do the second way(correct way) in your pagelist.php, you will get the variable $pageList. Then just echo $pageList in your viewfile.

Sign up to request clarification or add additional context in comments.

2 Comments

This does not fix the problem. I still get the same error message that the variable is undefined.
The second option worked! Thanks a lot. This really helped me in understanding! Thxx

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.