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

PHP Datastorage Class (continued)
By: Chris Root
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 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:
      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


    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
     

       

    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 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek