Reputation: 1702
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.
Upvotes: 0
Views: 465
Reputation: 7475
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.
Upvotes: 2