Home arrow PHP arrow PHP Datastorage Class (continued)

PHP Datastorage Class (continued)

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.

TABLE OF CONTENTS:
  1. PHP Datastorage Class (continued)
  2. Utility Methods
  3. The Output Methods in Depth
  4. A Snack for the Road
By: Chris Root
Rating: starstarstarstarstar / 5
November 21, 2005

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: