Home arrow PHP arrow Page 4 - Defining a Model Class for Handling Views with CodeIgniter

Demonstrating a simple use of the model class - PHP

Welcome to the sixth installment of a seven-part series on handling views with the CodeIgniter PHP framework. This series shows you a few handy approaches that you can implement quickly within your CI-based programs to work with view files in a truly clever way, ranging from loading them sequentially to nesting views within other views.

TABLE OF CONTENTS:
  1. Defining a Model Class for Handling Views with CodeIgniter
  2. Review: moving presentation logic out of views
  3. Handling database contents with a simple model class
  4. Demonstrating a simple use of the model class
By: Alejandro Gervasio
Rating: starstarstarstarstar / 3
April 23, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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!



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 



© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap

Dev Shed Tutorial Topics: