PHP
  Home arrow PHP arrow Page 2 - Embedding Model Data in 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

Embedding Model Data in Views with Code Igniter
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 2
    2009-04-30


    Table of Contents:
  • Embedding Model Data in Views with Code Igniter
  • Review: the first two modules of the previous web application
  • Creating some basic view files
  • Finishing the sample PHP application with CodeIgniter

  • 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


    Embedding Model Data in Views with Code Igniter - Review: the first two modules of the previous web application
    ( Page 2 of 4 )

    Before I proceed to build the three view files required to complete the PHP application created in the previous tutorial of this series, it would be useful to list the signatures of its model and controller classes, to help you recall more quickly how they were built.

    That said, below I included these two classes. Have a look at them, please:

    (‘user_model.php’ file – located at /application/models/ folder)


    <?php

    class User_model extends Model{

    function User_model(){

    // call the Model constructor

    parent::Model();

    // load database class and connect to MySQL

    $this->load->database();

    }

    // fetch all users

    function getAll(){

    $query=$this->db->get('users');

    // return result set as an object array

    return $query->result();

    }

    // fetch some users based on a predefined condition

    function getWhere($field,$param){

    $this->db->where($field,$param);

    $query=$this->db->get('users');

    // return result set as an object array

    return $query->result();

    }

    // get total number of users

    function getNumber(){

    return $this->db->count_all('users');

    }

    }

    ?>



    (‘webpage.php’ file – located at /application/controllers/ folder)


    <?php

    class WebPage extends Controller{

    function WebPage(){

    // load controller parent

    parent::Controller();

    // load users model

    $this->load->model('User_model');

    // load some 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->User_model->getAll()),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);

    }

    }

    ?>


    Undeniably, the tasks performed by both the model and controller classes are very easy to follow. First, the model has a few simple methods for counting and retrieving rows from a “user” MySQL table. On the other hand, the controller implements an “index()” method that uses the model’s “getAll()” method to generate the contents section of a web page.

    Of course, it’s valid to point out here that each part of the web page being created is stored on a $data array, which is finally passed to a “main_page.php” view. All of these tasks, accomplished by the “WebPage” controller, naturally imply that the views need to be created accordingly.

    Therefore, in the following section I’m going to build the first three views, which will be tasked with generating the header, content and footer sections of the web document.

    To learn how this will be achieved, you’ll have to click on the link that appears below and read the section to come.



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