PHP
  Home arrow PHP arrow Page 2 - 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 - Review: moving presentation logic out of views
    ( Page 2 of 4 )

    If you’re like me, then you can’t wait to see how a model can be used for interacting with the “users” MySQL table that you saw in the prior tutorial. Before you learn that, however, I’m going to list all of the source files corresponding to the web application developed in the previous article, so you can clearly see the differences between using a model and using the CI’s database class directly.

    Having clarified that point, here’s the complete source code for this sample PHP program:

    (‘webpage.php’ file – located at /application/controllers/ folder)


    <?php

    class WebPage extends Controller{

    function WebPage(){

    // load controller parent

    parent::Controller();

    // load libraries here

    $this->load->database();

    // load helpers here

    }

    // generate web page using partial sections

    function index(){

    // generate headers section

    $data['header']=$this->load->view('header_view',array('header'=>'Header Section'),TRUE);

    $query=$this->db->get('users');

    if($query->num_rows > 0){

    // generate content section

    $data['content']=NULL;

    foreach($query->result() as $user){

    $data['content'].=$this->load->view('users_view',array('user'=>$user),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);

    }

    }

    ?>



    (‘main_page.php’ file - located at /application/views/ folder)


    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml">

    <head>

    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

    <title>Sample Web Page</title>

    <style type="text/css">

    body{

    padding: 0;

    margin: 0;

    background: #999;

    }

    #container{

    width: 600px;

    margin: 0 auto;

    }

    #header{

    padding: 10px;

    background: #eee;

    }

    #content{

    padding: 10px;

    background: #9cf;

    }

    #footer{

    padding: 10px;

    background: #eee;

    }

    h1{

    font: bold 2em Arial, Helvetica, sans-serif;

    margin: 0 0 18px 0;

    color: #039;

    }

    h2{

    font: bold 1.5em Arial, Helvetica, sans-serif;

    margin: 0 0 18px 0;

    }

    p{

    font: normal .8em Arial, Helvetica, sans-serif;

    margin: 0 0 18px 0;

    }

    </style>

    </head>

    <body>

    <div id="container">

    <?php

    echo $header.$content.$footer;

    ?>

    </div>

    </body>

    </html>



    (‘users_view.php’ file - located at /application/views/ folder)


    <p><strong>First Name: </strong><?php echo $user->firstname;?></p>

    <p><strong>Last Name: </strong><?php echo $user->lastname;?></p>

    <p><strong>Email: </strong><?php echo $user->email;?></p>

    <hr />



    (‘footer_view.php’ file - located at /application/views/ folder)


    <div id="footer">

    <h2><?php echo $footer;?></h2>

    <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut.</p>

    </div>

    In this case, three views and a basic web page controller class are the only building blocks required for developing a web application whose main task is to display the contents of the “users” MySQL table on screen. As I stated before, the controller directly uses the database class for retrieving database rows, which are properly parsed before being embedded into the “main_page.php” view file.

    Undoubtedly, the above approach demonstrates in a nutshell how to partially remove some presentation logic from a view, but it’s not the only method that you may want to use for handling views with CodeIgniter.

    Now that you've hopefully recalled how the web application shown a few lines before was built by using a single controller class and some additional views, it’s time to move on. We're going to start defining a users model class, which will be tasked with retrieving database rows from the already familiar “users” MySQL table. Of course, creating a model implies that the signature of the controller must be modified to work with it, but don’t worry about this for the moment. Simply proceed to read the following section, where I’m going to explain how to build this model class.

    Now, click on the link that appears below and read the next segment.



     
     
    >>> 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 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek