PHP
  Home arrow PHP arrow Page 4 - 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

Handling Views with CodeIgniter
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 6
    2009-03-19


    Table of Contents:
  • Handling Views with CodeIgniter
  • Preparing views to be rendered on screen
  • Creating a content view
  • Building a web page controller 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


    Handling Views with CodeIgniter - Building a web page controller class
    ( Page 4 of 4 )

    To be frank, generating an entire web document by sequentially loading the three views that you saw before is a no-brainer process that you’ll grasp in a snap. But how can it be done? Well, first it’s necessary to define a controller class that performs this specific task. In this case, since I’m attempting to create a web page, the controller will be called “WebPage” (my creativity with names sometimes blows me away) and it’ll look like this:


    <?php

    class WebPage extends Controller{

    function WebPage(){

    // load controller parent

    parent::Controller();

    // load libraries here

    $this->load->database();

    // load helpers here

    }

    // load views sequentially

    function index(){

    // load 'header' view

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

    // load 'content' view and pass in database content

    $this->load->view('content_view',array('users'=>$this->db->get('users')));

    // load 'footer' view

    $this->load->view('footer_view',array('footer'=>'Footer Section'));

    }

    }

    ?>


    See how simple it is to build a web page by loading views in a sequential manner? The controller defined above demonstrates this process pretty clearly, since it firsts loads the CI’s database class within its constructor, and then  proceeds to build the different sections of the page by loading in turn each of the previous views. Simple and efficient.

    Now that you hopefully understood how the “WebPage” controller works, save it under the “/application/controllers/ folder and type the following URL on your browser’s address field to test it: http://localhost/codeigniter/index.php/webpage/


    If everything goes well, you should get the following output on screen:



    There you have it. At this stage you've learned how to sequentially load three different views to generate a basic web page. Logically, there’s plenty of room to improve the signature of the controller or even the views themselves. But guess what? This will be left as homework for you.

    Final thoughts

    In this first episode of the series, I explained how to build different sections of a basic web page with CodeIgniter by sequentially loading three independent view files. It could be said that this approach is possibly the simplest to implement when handling views with CI, but as I said in the beginning, it’s not the only one.

    In the next article, I’ll be polishing the visual appearance of the web page generated previously to make it look slightly more appealing. Now that you’ve been warned about the topic that will be covered in the forthcoming part of the series, don’t miss it!



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