PHP
  Home arrow PHP arrow PHP Datastorage Class (continued)
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

PHP Datastorage Class (continued)
By: Chris Root
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 5
    2005-11-21

    Table of Contents:
  • PHP Datastorage Class (continued)
  • Utility Methods
  • The Output Methods in Depth
  • A Snack for the Road

  • 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


    PHP Datastorage Class (continued)


    (Page 1 of 4 )

    In the first part of this two-part article, you started to learn about using alternatives to databases for storing data; specifically, we started to work on creating a class that can handle flat files, session variables, and cookies. This second part picks up right where we left off last time.

    Modes 3 - 7

    This is where things get interesting. Mode 3 will output a representation of the data using an XSLT style sheet. The data is first converted to a simple XML format and then processed using a style sheet specified by the second argument of the readout method. Producing an attractive representation of the contents of a shopping cart, for instance, is only a matter of crafting the appropriate XSLT style sheet. You can even do simple math with XSLT, which makes handling prices a breeze. Both the Sablotron-based XSLT and DOMXML extensions are supported. Support for the much improved XSL extension in PHP 5 would not be difficult to add.

    Mode 4 outputs the afore mentioned XML format. There certainly could be additional possibilities here depending on your needs for XML output.

    The mode is 5 readout uses the print_r function to output a raw text version of its data contents. This can be invaluable for debugging purposes.

    If the data you are storing lends itself to being viewed in a spreadsheet, mode 6 will output all data in the comma delimited CSV format, which can be read by several spreadsheet applications, including Microsoft Excel, and can be used to load database tables using MySQL or other database systems.

    The final mode (mode 7) will output a very simple configuration file format.

    #*************
    #user_settings
    #*************
    param1=foo
    param2=foobar
    param3=barfoo
    #*************
    #system_settings
    #*************
    param1=foo
    param2=foobar
    param3=barfoo

    I'll discuss the actual methods that output these formats in a moment, but first we should probably have a few more ways to get at our data. The get_item method will retrieve a single item (row) or specific field value from the datastore. The first argument is the items key and the second optional argument is a specific field within that row to retrieve.

    function get_item($id,$field="")
    {
          if($field == "")
          {
                if(isset($this->items[$id]))
                {
                      return $this->items[$id];
                }
                else
                {
                      return false;
                }
          }
          else
          {
                if(isset($this->items[$id][$field]))
                {
                      return $this->items[$id][$field];
                }
                else
                {
                      return false;
                }
          }
    }

    This method returns false if it can't find the item for which you are looking. It has a number of expansion possibilities using regular expressions or perhaps some sort of query parser. The final output method is by_value.

    function by_value($val)
    {
          $len = $this->get_count();
          if($len > 0)
          {
                $keys = array_keys($this->items);
                for($i = 0;$i < $len;$i++)
                {
                      if(array_search($val,$this->items[$keys[$i]]))
                      {
                            $ret[$keys[$i]] = $this->items[$keys[$i]];
                      }
                }
                if(count($ret) > 0)
                {
                      return $ret;
                }
                else
                {
                      return false;
                }
          }
          else
          {
                $this->err("No items in Datastore");
                return false;
          }
    }

    This method takes a single argument, which is a string value to search for within the datastore. It returns an array of all rows that contain that value, such as all the rows that contain web page hit counts for a specific company or section of a site.

    One final output method that is independent of the readout method is sql_template. The sql_template method accepts an id of an item in the datastore and a specially formatted sql statement that works much like an html template.

    function sql_template($id="",$sql="")
    {
          if($id != "" && $sql != "")
          {
                if($item = $this->get_item($id))
                {
                $ret = $sql;
                      $keys = array_keys($item);
                      for($i = 0;$i < count($keys);$i++)
                      {
                            $ret = str_replace($keys[$i],$item[$keys[$i]],$sql);
                      }
                      return $ret;
                }
                else
                {
                      $this->err("Item not found",1);
                      return false;
                }
          }
          else
          {
                $this->err("Required arguments not supplied",1);
                return false;
          }
    }

    //*********************************
    EXAMPLE

    sql statement:
    “SELECT * FROM [table] WHERE [key_field] = [value]”;

    item array in datastore
    [table] => products
    [key_field] => product_id
    [value] >> 25445587

    In the above example, if you have an array key called “[table]”, the value associated with that key is dropped in the place of the string “[table]” in the sql template. Square brackets are used most often for templates. Square brackets are also part of sql syntax, but as long as you have your key names matched up with the strings to replace in the template, you shouldn’t have any trouble.

    As with the store method you can certainly use readout as well as any of the other output methods multiple times in the same document for different tasks.

    More PHP Articles
    More By Chris Root


       · Even though this is the first of two parts, don't be afraid to comment on it and ask...
       · Hello everyone. I hope you like the article. I have since added a few things to this...
       · Thanks for the article. Have to read it thorough a couple of times to comprehend...
       · Thank you. The datastore class was unusual in that it started as a simple component...
       · Hi, As a new comer to PHP I was looking for a set of utility classes to assist...
     

       

    PHP ARTICLES

    - 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
    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...
    - Working with the Email Class in Code Igniter





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