PHP
  Home arrow PHP arrow Page 3 - Moving Presentation Logic Out of Views with Code Igniter
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

Moving Presentation Logic Out of Views with Code Igniter
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 2
    2009-04-16


    Table of Contents:
  • Moving Presentation Logic Out of Views with Code Igniter
  • Review: returning values with a previous method
  • Moving presentation logic out of views
  • The modified source code of the sample PHP program

  • 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


    Moving Presentation Logic Out of Views with Code Igniter - Moving presentation logic out of views
    ( Page 3 of 4 )

    Since in this case I’m planning to move the “foreach” loop included within the “content_view.php” file out of it, the simplest way to do that is by implementing this part of the application straight into the controller. To demonstrate how to achieve this, below I listed the modified signature of the “WebPage” controller class, which now looks like this:


    <?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);

    }

    }

    ?>


    As shown above, the logic implemented by the “WebPage” controller practically remains the same, except for a tiny detail: now the “foreach” loop included previously within the “content_view.php” file has been moved within the controller, and in consequence it’s necessary to create a new view that displays the data retrieved from the “users” MySQL table.

    The signature of this new view file, called ”users_view.php,” looks as simple as this:


    <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 />


    Here you have it. Now the controller is responsible for looping over the rows fetched from the MySQL table, and the result is stored on the $data[‘content’] array element. Lastly, each section of the web page is embedded into the “main_page.php” view, which finishes rendering the whole web document. Not too hard to understand, right?

    Having implemented an alternative approach to building a basic MySQL-based web application, now it’s time to put all of its pieces together, since two of its files were modified. Thus, in the last section of this tutorial I’ll be listing for you the full source code of this sample PHP program, so you can copy/paste it and introduce your own improvements.

    Now, jump forward and read the next few lines. We’re almost finished!



     
     
    >>> 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