PHP
  Home arrow PHP arrow Page 3 - Building a Content Management System 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

Building a Content Management System with Code Igniter
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 6
    2008-10-08


    Table of Contents:
  • Building a Content Management System with Code Igniter
  • Creating sample MySQL tables and defining a model class
  • Building a controller class
  • Completing the CMS with view files

  • 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 Content Management System with Code Igniter - Building a controller class
    ( Page 3 of 4 )

    True to form, building a controller class that fetches all the movies included in the previous “movies” MySQL table, and also allows users to enter comments about each of them, simply involves defining a different method for performing each of these tasks.

    However, you will gain a better understanding of the way that this controller class is going to work if you examine its signature, which has been included below. Take a look at it, please:

    class Movies extends Controller{

    function Movie(){

    // load controller parent

    parent::Controller();

    // load 'MovieModel' model

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

    // load helpers

    $this->load->helper('url');

    $this->load->helper('form');

    }

    // display all movies

    function index(){

    $data['title']='Movies List';

    $data['movies']=$this->MovieModel->fetchAllRows('movies');

    // load 'movie_view' view

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

    }

    // display all comments

    function comments(){

    $data['title']='Comment List';

    $data['comments']=$this->MovieModel->fetchRow($this->uri->segment(3),'movie_id','movie_comments');

    // load 'moviecomment_view' view

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

    }

    // insert comment

    function insert_comment(){

    $this->MovieModel->insertRow('movie_comments',$_POST);

    redirect('movie/comments/'.$_POST['movie_id']);

    }

    }


    Although the definition of the above “Movie” controller looks pretty intimidating at first, it's actually pretty simple to understand. First, its constructor loads all of the source classes and functions that will be used within the management system; this includes the corresponding model, as well as the “url” and “form” helpers that you learned in previous articles of this series.

    Now, take a look at the “index()” method. As you’ll recall, it will be called automatically by Code Igniter when implemented, and it is tasked with displaying on screen all of the movies included in the “movies” MySQL table defined before.

    As you can see, this method first fetches all of the movies from the aforementioned table by means of the model, and lastly it embeds this data into a view file called “movie_view.php” for displaying purposes. For right now, don't worry about how this view looks, since this topic will be discussed in depth in the following section.

    At this level, you hopefully grasped the logic implemented by the pertinent “index()” method, right? So focus your attention now on the one called “comments().” As its name suggests, this method is responsible for fetching all of the comments that belong to a specific movie. This task is performed by means of a conditional WHERE SQL statement, which is logically hidden behind the model’s API.

    In addition, you should notice that this method retrieves the comments made on a particular movie by using a parameter passed in within the URL, whose value is returned by the following expression:

    $this->uri->segment(3)


    Actually, Code Igniter will automatically load a class called “uri” with each controller defined, so in this case its “segment()” method is utilized to retrieve the ID of the movie that has received a comment.

    And finally, the “insert_comment()” method again uses the model’s API, this time for adding a new entry to the “comments” MySQL table created in the previous section. Of course, the text and author that correspond to each comment are collected via a post HTML form, therefore the following expressions:

    $this->MovieModel->insertRow('movie_comments',$_POST);

    redirect('movie/comments/'.$_POST['movie_id']);


    first add the comment in question to the corresponding “comments” MySQL table, and then redirect users back to the comment’s web page.

    Now, before I forget, please save the controller class to the Code Igniter /system/application/controllers/ as “movies.php” folder for further use.

    So far, so good, right? At this point, you should have a clear idea of how the previous “Movies” controller class does its thing. Also, it’s possible that you still have some doubts regarding the way that movies and comments are displayed on the browser, or even how new comments are submitted by a user.

    However, you shouldn’t be too concerned about this. In the next section, I’ll be listing all of the view files that are required to perform all the aforementioned tasks.

    Want to see how this will be done? Click on the link below and keep reading.



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