Home arrow PHP arrow Page 3 - Defining a Model Class for Handling Views with CodeIgniter

Handling database contents with a simple model class - PHP

Welcome to the sixth installment of a seven-part series on handling views with the CodeIgniter PHP framework. This series shows you a few handy approaches that you can implement quickly within your CI-based programs to work with view files in a truly clever way, ranging from loading them sequentially to nesting views within other views.

TABLE OF CONTENTS:
  1. Defining a Model Class for Handling Views with CodeIgniter
  2. Review: moving presentation logic out of views
  3. Handling database contents with a simple model class
  4. Demonstrating a simple use of the model class
By: Alejandro Gervasio
Rating: starstarstarstarstar / 3
April 23, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

In the preceding segment, I mentioned that it was perfectly possible to incorporate a model class to access database contents, instead of directly using the database class included with CodeIgniter. So, to put this theoretical concept into practice, below I created such a class. In this case, it is charged with fetching some rows from the “users” MySQL table that you learned before, and with counting its records as well.

In summary, this model class, called “User_model,” is defined in the following way:


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

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 object

return $query->result();

}

// get total number of users

function getNumber(){

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

}

}

?>


In reality, understanding how the above “User_model” class works is a pretty straightforward process, don’t you think? As you can see, this model implements a few basic methods for retrieving users from the corresponding database table, and for counting them as well. Naturally, it’s feasible to aggregate more methods that perform inserts, updates and deletions, but for now I’ll keep the signature of the model simple.

So far, so good. At this point, I showed you how to build a basic model class that will be responsible for fetching user-related data from the pertinent MySQL table. Therefore, the next step is to change the signature of the “WebPage” controller class so that it can use the model for accessing database contents.

This modification will be discussed in detail in the last section of this tutorial. Therefore, please click on the link below and read the upcoming segment.



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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: