Since in the previous section I created a basic model class that permits us to fetch and count users stored on a MySQL table, the next thing I’m going to do will be modifying the signature of the corresponding “WebPage” controller so that it can use the model’s methods from behind its API. Having explained that, here’s how the controller now looks: <?php class WebPage extends Controller{ function WebPage(){ // load controller parent parent::Controller(); // load users model $this->load->model('User_model'); // load some helpers here } // generate web page using partial sections function index(){ // generate header section $data['header']=$this->load->view('header_view',array('header'=>'Header Section'),TRUE); // generate content section $data['content']=$this->load->view('content_view',array('users'=>$this->User_model->getAll()),TRUE); // generate footer section $data['footer']=$this->load->view('footer_view',array('footer'=>'Footer Section'),TRUE); // generate full web page $this->load->view('main_page',$data); } } ?> Simple to code and read, isn’t it? As shown above, now the “index()” method of the controller generates the header, body and footer parts of a web page, but this time the body section is populated with database contents that are fetched via the “getAll()” method that belongs to the model. At this stage, not only does the controller show how to utilize the methods given by a specific model, but the example illustrates how to assemble an entire web page by returning different strings from the “$this->load->view()” method. The missing pieces of this schema are the three view files that actually render the web page in question. However, these will be created in the last article of the series. Meanwhile, feel free to edit and enhance the respective signatures of the model and the controller to acquire a more solid grounding in developing database-driven applications with CodeIgniter. Final thoughts Over this sixth episode of the series, I went through building a basic model class with CodeIgniter. This class was incorporated into a simple PHP application whose primary functionality was displaying information on screen about some users stored on a MySQL table. Logically, at this point the application is still incomplete, since it is necessary to create three different views that will be responsible for generating independently the header, main area and footer section of the web page where user-related data will be echoed. Those views will be built in the last tutorial. So here’s a piece of advice that you should consider seriously: don’t miss the final article!
blog comments powered by Disqus |
|
|
|
|
|
|
|