Miscellaneous Code

  Home arrow Miscellaneous Code arrow Page 4 - A Web App Based on a Model for the Cod...
MISCELLANEOUS CODE

A Web App Based on a Model for the CodeIgniter PHP Framework
By: Alejandros Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 2
    2009-09-30

    Table of Contents:
  • A Web App Based on a Model for the CodeIgniter PHP Framework
  • Review: the generic model class’s complete source code
  • Inheriting the functionality of the abstract model class
  • The property model class in action

  •  
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    advertisement

    A Web App Based on a Model for the CodeIgniter PHP Framework - The property model class in action


    (Page 4 of 4 )

    As I expressed in the previous segment, it’s necessary to define a controller that takes advantage of the functionality provided by the “Property_model” class to fetch, save and delete properties in the corresponding database table. This is how we will construct our simple real estate web program.

    Basically, the signature of this properties controller class looks like this:

    <?php

     

    class Properties extends Controller

    {

    function Properties()

    {

    parent::Controller();

    // loads property model

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

    }

    // fetches all the properties

    function index()

    {

    if ($query = $this->Property_model->fetch())

    {

    $data['properties'] = $query;

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

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

    }

    // Otherwise display errors

    else

    {

    $this->load->view('error_view', array('error' => $this->Property_model->getErrors()));

    }

    }

     

    // Inserts a new property

    function save()

    {

    $this->Property_model->setData(array('title' => 'Sample Property', 'short_description' => 'This is the description of the sample property'));

    if ($this->Property_model->save())

    {

    redirect('/properties');

    }

    // Otherwise display errors

    else

    {

    $this->load->view('error_view', array('error' => $this->Property_model->getErrors()));

    }

    }

     

    // Updates an existing property

    function update()

    {

    $this->Property_model->setID(1);

    $this->Property_model->setData(array('title' => 'Test', 'Sample Property' => 'This is another description of the sample property'));

    if ($this->Property_model->save())

    {

    redirect('/properties');

    }

    // Otherwise display errors

    else

    {

    $this->load->view('error_view', array('error' => $this->Property_model->getErrors()));

    }

    }

     

    // Deletes an existing property

    function delete()

    {

    $this->Property_model->setID(1);

    if ($this->Property_model->delete())

    {

    redirect('/properties');

    }

    // Otherwise display errors

    else

    {

    $this->load->view('error_view', array('error' => $this->Property_model->getErrors()));

    }

    }

    }

    // END Properties Class

     

    /* End of file Properties.php */

    /* Location: ./system/application/controllers/properties.php */

    In this case, a single controller class is all that I need to implement a basic CMS system that handles properties for a real estate application. As you can see, the controller’s constructor simply loads the “Property_model” class, while the “index()” method uses this model to list all of the properties available in the “properties” MySQL table.

    On the other hand, the “save(),” “update()” and “delete()” methods, as their names suggest, are used to insert, update and delete properties in the corresponding table. Besides, it’s worthwhile to note that each of these methods will load an error view if a failure occurs, which helps to code the application in a more defensive way. And finally, to complete the previous example, here are the view files used to display properties and errors respectively:

    (‘properties_view.php’ file)

     

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

    <html>

    <head>

    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

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

    </head>

    <body>

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

    <?php foreach ($properties as $property):?>

    <p><?php echo 'ID: ' . $property->id;?></p>

    <p><?php echo 'Title: ' . $property->title;?></p>

    <p><?php echo 'Description: ' . $property->short_description;?></p>

    <hr />

    <?php endforeach?>

    </body>

    </html>

     

     

    (‘error_view.php’ file)

     

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

    <html>

    <head>

    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

    <title><?php echo 'An error was found!';?></title>

    </head>

    <body>

    <h1><?php echo 'An error was found!';?></h1>

    <p><?php echo $error;?></p>

    </body>

    </html>

    Now that I have shown you the two view files that display errors and property information, the development of this sample real estate web application is complete. Finally, if you wish to try out this program with your own CodeIgniter installation, don’t forget to load the database class either in the controller or automatically to get the program working as expected.

    Final thoughts

    It’s hard to believe, but we’ve come to the end of this hopefully educational journey. Overall the experience has been fun and instructive too, since in different tutorials you learned how to build an abstract model class for the CodeIgniter PHP framework with minor efforts. From this point on you can either pick up more complete model libraries like DataMapper and IgnitedRecord, or you can use the previous sample model class and introduce your own enhancements according to your specific requirements.

    See you in the next PHP development tutorial!


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.
    blog comments powered by Disqus

    MISCELLANEOUS CODE ARTICLES

    - Creating a Web Page Controller with the HMVC...
    - Coding Controllers and Views for the HMVC De...
    - A Sample Web Application with the HMVC Desig...
    - Adding a Class to Parse Views to an HMVC Des...
    - Building a Model Class for the HMVC Design P...
    - Filtering Input Data and Generating HTML For...
    - The HMVC Design Pattern: Working with MySQL ...
    - Dispatching Requests to MVC Triads with the ...
    - Implementing the Hierarchical Model-View-Con...
    - A Web App Based on a Model for the CodeIgnit...
    - Completing a Model for the CodeIgniter PHP F...
    - Validating Input Data with the CodeIgniter P...
    - Deleting Database Records with the CodeIgnit...
    - Inserting Database Records with a CodeIgnite...
    - Fetching Database Rows with a Model for the ...


    © 2003-2012 by Developer Shed. All rights reserved. DS Cluster 1 - Follow our Sitemap