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

Returning Strings from Views with Code Igniter
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 1
    2009-04-09


    Table of Contents:
  • Returning Strings from Views with Code Igniter
  • Review: using nested views along with the load-vars() method
  • Generating partial sections of a web page
  • Putting all of the views together

  • 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


    Returning Strings from Views with Code Igniter - Putting all of the views together
    ( Page 4 of 4 )

    In order to build a complete web page with the views that you saw in the previous section, it’s necessary to define a controller class that first stores the strings returned from each of them, and then passes this content to the layout view. If this sounds a bit confusing to you, the following web page controller should help to clarify any issues that you might have. Here it is:


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

    }

    }

    ?>


    Do you see how simple it is to generate a dynamic web page by returning strings with the “$this->load->view()” method? I guess you do. In the above example, each of the sections that make up the page are stored temporally on the $data array, and then it’s passed to the “main_page” view for displaying purposes.

    Hopefully, this code sample should give you a clear idea of how to return contents from a view after they've been loaded, via the third “TRUE” argument as shown before.

    And finally, assuming that all of this has been set up correctly, if you type the following URL into your browser:


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


    Then you should get the following output:



    Here you have it. At this point, you've learned how to use the “$this->load->view()” method in a slightly different way to generate a simple database-driven web application with CodeIgniter. All of the examples developed in this tutorial can be enhanced. For example, you might want to create a users model and utilize it within the controller to fetch database rows, instead of using the database class directly.

    Whatever you decide to do, one thing is certain: CodeIgniter will keep you entertained for hours!

    Final thoughts

    In this fourth part of this series, I explained how to build a simple, yet dynamic, web page by returning strings from some views. As was illustrated earlier, this approach permits us to generate partial sections of a web document in a pretty intuitive and flexible fashion, and I’m sure that you’ll find even more useful ways to use it.

    In the following article, I’ll be discussing how to make views even more "dumb" (if the term is actually applicable) by removing certain presentation logic from them. If you wish to learn how this will be done, don’t miss the upcoming tutorial!



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