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