PHP
  Home arrow PHP arrow Page 2 - Building a Database-Driven Application...
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
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: 5 stars5 stars5 stars5 stars5 stars / 2
    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:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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 - Retrieving user-related data from a MySQL table


    (Page 2 of 4 )

    As I anticipated in the beginning, the first step involved in the development of this MySQL-driven application will require defining a model class. In crude terms, this class will act as a simple interface for retrieving user-related data from a sample “users” MySQL table.

    To clarify a bit more how this class is going to work, please take a look at its signature, which is listed below:


    class Users extends Model{

    function Users(){

    // call the Model constructor

    parent::Model();

    // load database class and connect to MySQL

    $this->load->database();

    }

    function getAllUsers(){

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

    if($query->num_rows()>0){

    // return result set as an associative array

    return $query->result_array();

    }

    }

    function getUsersWhere($field,$param){

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

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

    // return result set as an associative array

    return $query->result_array();

    }

    // get total number of users

    function getNumUsers(){

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

    }

    }


    As shown above, the previous “Users” class extends the model that comes included with Code Igniter, and has been provided with a set of intuitive methods that come in useful for retrieving some rows from a “users” database table. As you can see, the class in question is capable of fetching one row at a time and multiple records as well, and additionally has the ability to count the number of rows contained within a result set.

    However, the most interesting facet of this model class is the way that it implements all of its methods. In this particular case, the constructor first loads the database class bundled with Code Igniter via a loader object, and then connects to MySQL using the set of parameters specified in the “database.php” file that you learned in the previous article.

    From this point onward, the model uses some methods provided by the aforementioned database class to fetch user data from the corresponding “users” MySQL table.

    Of course, I’m not going to list the complete API corresponding to the Code Igniter database class, since you can visit its web site for a full reference on them. But here’s a brief description of the methods used by the previous “Users” model:


    $this->db->get('table_name'): get all the rows from the specified MySQL table


    $this->db->where($field,$param): performs a “where” query against the specified MySQL table


    $this->db->count_all('table_name'): count all the rows in the specified MySQL table


    $query->result_array(): returns a result set as a multidimensional array


    $query->num_rows(): counts the number of row returned in a result set


    As you can see, the built-in Code Igniter database class includes a group of straightforward methods, which can be used to perform the most common database-related tasks, such as fetching and counting rows, handling data sets, and so forth. As I stated before, however, you should read Code Igniter’s user guide for further information about how these methods work.

    Well, at this point I showed you how to create a simple “Users” model class that can be utilized for retrieving and counting rows from sample MySQL table. Before I forget, you should save the model to the /system/application/models/ folder of Code Igniter, so it can load it correctly later on.

    Provided that you grasped the logic that drives this class, the next thing that I’m going to teach you will be how to build a controller class. This class will perform a few useful tasks, such as retrieving information from the table by using the model’s API, and embedding this data straight into a view file.

    To learn how this controller class will be built, please jump forward and read the following section.

    More PHP Articles
    More By Alejandro Gervasio


       · In this second part of the series, you’ll get started using some core classes...
       · Hello, Alejandro i just ask you What do you think of this outdated php4 style, which...
       · I get a class redefined error with the code presented in the article. The problem is...
       · There doesn't appear to be any indentation in the sample code, making it very hard...
       · Thanks for the kind comments on my PHP article. As I explained in the first article,...
       · Thanks for the kind comments on my PHP article. Sorry to hear about code indentation...
       · Thanks for the kind words on my PHP article. Yes, your code is a correct...
     

       

    PHP ARTICLES

    - Authentication Scripts for a User Management...
    - Utilizing the Use Keyword for Namespaces in ...
    - Building a User Management Application
    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Database Security: Guarding Against SQL Inje...
    - Building a Modular Exception Class in PHP 5
    - Database and Password Security for Web Appli...
    - Handling MySQL Data Set Failures in PHP 5
    - Building Site Registration for Web Applicati...
    - Intercepting Customized Exceptions in PHP 5
    - Securing Your Web Application Against Attacks
    - Sub Classing Exceptions in PHP 5
    - Authentication for Web Application Security





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
    Stay green...Green IT