PHP
  Home arrow PHP arrow Page 2 - Inserting, Updating and Deleting Database Rows 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

Inserting, Updating and Deleting Database Rows with Code Igniter
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 7
    2008-09-24


    Table of Contents:
  • Inserting, Updating and Deleting Database Rows with Code Igniter
  • Performing conditional SELECT statements with Code Igniter
  • Inserting new rows into a selected MySQL table
  • Updating and deleting database records using the active record pattern

  • 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


    Inserting, Updating and Deleting Database Rows with Code Igniter - Performing conditional SELECT statements with Code Igniter
    ( Page 2 of 4 )

    Actually, before I proceed to demonstrate how to perform inserts, updates and deletions with the active record pattern, I’d like to reintroduce a pair of practical examples developed in the previous tutorial. They showed how to run conditional SELECT statements via this pattern.

    Each of these examples was comprised naturally of three source files: a model, a controller class, and finally a view file. All of them worked with a sample “users” MySQL table, whose structure was similar to the following:



    Okay, now that you remember how the table looked, here’s the complete source code corresponding to the first example. It fetches all of the users whose IDs are less than 5. Take a look at it, please:

    (definition for ‘users.php’ file - located at /system/application/models/ folder)


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

    }

    }



    (definition for ‘users.php’ file - located at /system/application/controllers/ folder)


    class Users extends Controller{

    function Users(){

    // load controller parent

    parent::Controller();

    // load 'Users' model

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

    }

    function index(){

    $data['users']=$this->Users->getUsersWhere('id <',5);

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

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

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

    // load 'users_view' view

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

    }

    }



    (definition for ‘users_view.php’ file - located at /system/application/views/ folder)


    <html>

    <head>

    <title><?php echo $title;?></title>

    </head>

    <body>

    <h1><?php echo $header;?></h1>

    <ul>

    <?php foreach($users as $user):?>

    <li>

    <p><?php echo 'Full Name: '.$user['firstname'].' '.$user['lastname'].' Email: '.$user['email'];?></p>

    </li>

    <?php endforeach;?>

    </ul>

    <p><?php echo 'Total number of users :'.$numusers;?></p>

    </body>

    </html>

    As you can see, the three source files listed above are the building blocks of a basic MySQL-driven application. Its goal is to retrieve all of the rows included in the previous “users” table whose ID values are less than 5. Obviously, this is only a primitive example, but it shows how to use the active record pattern to perform conditional SELECT statements with Code Igniter.

    In a similar fashion, the following sample application fetches all the users whose IDs are greater than 2. Here are its corresponding source files.

    (definition for ‘users.php’ file - located at /system/application/models/ folder)


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

    }

    }



    (definition for ‘users.php’ file - located at /system/application/controllers/ folder)


    class Users extends Controller{

    function Users(){

    // load controller parent

    parent::Controller();

    // load 'Users' model

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

    }

    function index(){

    $data['users']=$this->Users->getUsersWhere('id >',2);

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

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

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

    // load 'users_view' view

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

    }

    }



    (definition for ‘users_view.php’ file - located at /system/application/views/ folder)


    <html>

    <head>

    <title><?php echo $title;?></title>

    </head>

    <body>

    <h1><?php echo $header;?></h1>

    <ul>

    <?php foreach($users as $user):?>

    <li>

    <p><?php echo 'Full Name: '.$user['firstname'].' '.$user['lastname'].' Email: '.$user['email'];?></p>

    </li>

    <?php endforeach;?>

    </ul>

    <p><?php echo 'Total number of users :'.$numusers;?></p>

    </body>

    </html>

    Having listed the full source code for the two previous web applications, I should assume that running conditional SQL queries using the active record pattern is now a familiar topic to you, right? Thus, it’s time to learn how to use this pattern to perform inserts, updates and deletions against a selected database table.

    As you may guess, this subject will be covered in depth in the following section. Please read the next few lines.



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

       

    PHP ARTICLES

    - 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...
    - Mastering WHILE Loops for PHP and MySQL





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek