PHP
  Home arrow PHP arrow Page 2 - 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 - Review: returning values with a previous method
    ( Page 2 of 4 )

    In the introduction, I mentioned that it’s possible to remove from a view the PHP loop that iterates over the database rows, based on the structure of the web application developed in the previous article. But before I show you how to do that, I’m going to reintroduce the full source code of this sample PHP program, so you can recall how it was developed initially.

    That being said, here are all the files that comprise this web application:


    (‘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 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->db->get('users')),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>



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


    <div id="header">

    <h1><?php echo $header;?></h1>

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

    </div>



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


    <div id="content">

    <?php if($users->num_rows > 0):?>

    <?php foreach($users->result() as $user):?>

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

    <?php endforeach;?>

    <?php endif;?>

    </div>



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


    Having listed all of the source files that compose the MySQL-driven application built during the previous tutorial of this series, it’s worthwhile to stress one point regarding the way it’s been structured: as you can see, within the “content_view.php” view file there’s a “foreach” construct that loops over the rows fetched from a “users” MySQL table.

    This is a valid approach, used frequently for constructing views. However, it’s also feasible to implement a little twist here and remove the loop from this specific view, in this way increasing the level of separation between the application’s logic and its visual presentation even more.

    Provided that you’re interested in learning how to turn the concept deployed above into functional code, in the next section I’ll be creating a brand new view file, in addition to modifying slightly the signature of the “WebPage” controller class.

    Click on the link below and continue reading.



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

       

    PHP ARTICLES

    - Implementing the Data Mapper Design Pattern ...
    - Defining an Abstract Class with Restrictive ...
    - The Reflection API: Working with Reflected M...
    - Using Restrictive Constructors in PHP 5
    - Getting Information on a Reflected Class wit...
    - Introducing the Reflection API in PHP 5
    - Swift Mailer's Batchsend Method and Other Fe...
    - Embedding Attachments into Email Messages wi...
    - Dynamically Attaching Files with Swift Mailer
    - Using Different Paths for Attachments with S...
    - Handling Attachments and Sending HTML Email ...
    - Sending Blind Carbon Copies with Swift Mailer
    - Using Swift Mailer's Cc MIME Header
    - Using Sendmail and Mail() with Swift Mailer
    - Using Different SMTP Transports with Swift M...


    Code Analysis Tools
    Enterprise code analysis tools that deliver quality and reliable code



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