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

Using Nested Views with CodeIgniter
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 6
    2009-04-02


    Table of Contents:
  • Using Nested Views with CodeIgniter
  • Review: loading views sequentially with CodeIgniter
  • Preparing a nested view for displaying database contents
  • Embedding actual data into views with 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


    Using Nested Views with CodeIgniter - Embedding actual data into views with a web page controller class
    ( Page 4 of 4 )

    In the previous section, you learned how to build a view file that also includes other partial views. However, it’s necessary to create a controller class that implements the logic required to generate a complete web page, which will be filled in with some database contents.

    In this specific case, the controller that I’m going to define below will use the “$this->load->vars()” method included with CI’s loader class to replace the variables included within the views with actual data.

    That being explained, the web page controller looks like this:


    <?php

    class WebPage extends Controller{

    function WebPage(){

    // load parent controller

    parent::Controller();

    // load libraries here

    $this->load->database();

    // load helpers here

    }

    // generate web page with nested views

    function index(){

    // generate header, content and footer sections

    $data=array('header'=>'Header Section','users'=>$this->db->get('users'),'footer'=>'Footer Section');

    // load variables in views

    $this->load->vars($data);

    // load 'main_page' view

    $this->load->view('main_page');

    }

    }

    ?>


    Short to code and even simpler to read. Basically, all that the above “WebPage” controller does is  generate a simple web page whose main area has been populated with the contents of a “users” MySQL table. As I explained before, the replacement of variables included in the views is achieved with the $this->load->vars()” method, before loading the view that renders the main web document.

    In summary, if all has gone well, if you type the URL below into your browser’s address field:


    http://localhost/codeigniter/index.php/webpage


    Then you should get the following output:



    Hopefully with this simple example, you learned how to build a dynamic web page with CodeIgniter by using nested views along with the “$this->load->vars()” method. As usual with my articles on PHP web development, feel free to edit all of the code samples shown in this tutorial, so you can practice handling views with CodeIgniter.

    Final thoughts

    In this third installment of the series, I explained how to build a dynamic web page using a combination of CodeIgniter’s “$this->load->vars()” method and nested views. Hopefully you won’t have major trouble implementing this approach successfully when building your own PHP applications with CI.

    In the next chapter, I’m going to extend the concept of nested views a bit further by using a third parameter within the “$this->load->view()” method shown before.

    Now that you know what the upcoming article will be about, you can’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 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek