PHP
  Home arrow PHP arrow Page 4 - Defining a Model Class for Handling Views with CodeIgniter
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
PHP

Defining a Model Class for Handling Views with CodeIgniter
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 2
    2009-04-23


    Table of Contents:
  • Defining a Model Class for Handling Views with CodeIgniter
  • Review: moving presentation logic out of views
  • Handling database contents with a simple model class
  • Demonstrating a simple use of the model class

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Defining a Model Class for Handling Views with CodeIgniter - Demonstrating a simple use of the model class
    ( Page 4 of 4 )

    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
     

       

    PHP ARTICLES

    - Adding Ordering and Grouping Clauses to the ...
    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek