PHP
  Home arrow PHP arrow Page 3 - Building a Database-Driven Application with the Code Igniter PHP Framework
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? 
PHP

Building a Database-Driven Application with the Code Igniter PHP Framework
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 4
    2008-08-20


    Table of Contents:
  • Building a Database-Driven Application with the Code Igniter PHP Framework
  • Retrieving user-related data from a MySQL table
  • Defining a controller class
  • Outputting user-related data with Code Igniter

  • 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


    Building a Database-Driven Application with the Code Igniter PHP Framework - Defining a controller class
    ( Page 3 of 4 )

    In the section that you just read, I demonstrated how to build a model class, which came in helpful for retrieving data from a “users” MySQL table. It’s time to build the corresponding controller, which will use the API of the model to directly access this user-related data.

    Please examine the signature of this brand new controller class, which looks like this:


    class Users extends Controller{

    function Users (){

    // load controller parent

    parent::Controller();

    // load 'Users' model

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

    }

    function index(){

    // store data for being displayed on view file

    $data['users']=$this->Users->getUsers();

    $data['numusers']=$this->Users->getNumUsers();

    $data['title']='Displaying user data';

    $data['header']='User List';

    // load 'users_view' view

    $this->load->view('users_view',$data);

    }

    }


    As shown above, I created the previous user controller class by extending the default controller that comes bundled with Code Igniter. Again, don’t forget to save this file to the /system/application/controllers/ folder of Code Igniter for later use.

    Besides, there are some important details that you should notice with reference to the way that this class has been defined. First, note how the controller uses the corresponding loader class for loading the user model defined in the prior section.

    See how easy it is to instruct a controller to load a particular model? I guess you do! The required syntax for performing this task is the following:


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


    Simple and intuitive, right? Now that you have learned how to include a determined model from within a controller class, it’s time to pay attention to the implementation of the “index()” method, which actually is very interesting. As you can see, it uses the model’s API to fetch all the rows of the pertinent “users” database table, along with the number of records contained in the table in question.

    In addition, it’s very important to stress here the notation used for accessing the model’s API. Here’s an example of how to do this:


    $data['users']=$this->Users->getUsers();


    In this case, since the model has been named “Users,” Code Igniter automatically creates an object that can be referenced as $this->Users. Naturally, if there’s a model called “Blog,” it should be called within the controller as $this->Blog. That’s not too difficult to understand, right?

    Now, returning to the implementation of the “index()” method, it finishes its execution by defining some additional parameters, which are first stored in the $data array, and then passed to the corresponding view file for display purposes.

    Also, an additional explanation is in order here: please, notice how the controller reuses the loader object for loading the view and populating it with user-related data.

    So far, so good. At this stage, I showed you how to build a controller class, which uses the model’s API, to fetch some rows from a sample “users” MySQL table, and to embed this data into a view file.

    Nonetheless, if you’re like me, then at this moment you’re wondering how this view file actually looks. Well, in response to that question, in the following section I’ll be creating this view, completing the development of this MySQL-driven application.

    Please, click on the link below and keep reading. We’re almost done!



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

       

    PHP ARTICLES

    - 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...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    Stay green...Green IT