PHP
  Home arrow PHP arrow Page 5 - PHP Datastorage Class
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? 
PHP

PHP Datastorage Class
By: Chris Root
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 10
    2005-11-14


    Table of Contents:
  • PHP Datastorage Class
  • Top of the Page
  • Flat Files
  • Getting Data In
  • Getting Data Out

  • 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 - Getting Data Out
    ( Page 5 of 5 )

    One thing that our datastore doesn't have yet is a way to get the data back into the chosen storage container. This is where the store method comes in. When using this class you will always use store() before you are done. That isn't always easy to remember. I wrote this class, and yet, in the process of using it in an application that I was developing, I wondered why my data changes were not stored. Just remember the one thing they tell all new computer users: "SAVE YOUR WORK!"

    function store($mode="blank")
    {
    //$this->mode cannot go in the arguments as this will
    //throw a parse error. the if block below takes care of
    //setting a default.
         if($mode == "blank")
         {
               $mode = $this->mode;
         }
         if($mode == "s")
         {
               if(isset($_SESSION[$this->dataname]))
               {
                    $_SESSION[$this->dataname] = serialize($this->items);
               }
               else
               {
                    $this->err("There is no session variable by that name",1);
               }
         }
         else if($mode == "f")
         {
               if(file_exists($this->data_dir.$this->dataname.".dta"))
               {
                    if(is_writable($this->data_dir.$this->dataname.".dta"))
                    {
                         if($this->datafile = fopen($this->data_dir.$this->dataname.".dta","w"))
                         {
                               $write_str = serialize($this->items);
                               fwrite($this->datafile,$write_str);
                               fclose($this->datafile);
                         }
                         else
                         {
                               $this->err("Trouble Opening File for Writing",1);
                         }
                    }
                    else
                    {
                         $this->err("Data File not Writable",1);
                    }
               }
               else
               {
                    $this->err($this->data_dir.$this->dataname."Data File is Missing or Has Not Been Created Yet",1);
               }
         }
         else if($mode = "c")
         {
               if(isset($_COOKIE[$this->dataname]))
               {
                    $_COOKIE[$this->dataname] = serialize($this->items);
               }
         }
    }       

    If we are using a session variable, the store simply serializes the items array and stores it in the session. A series of if/else blocks attempts to ensure that all is okay and alert you if something goes wrong. Again as before, "f" is for file and "c" is for cookie. The cookie code in this case is pretty simple and is included in the example. You can use the store function more than once in your code. When the object is constructed, you may remember that we set a mode of operation for the storage, but it is possible to override this by providing an alternate storage method. This could allow you to go from one storage type to another, or to multiple ones within the same document.

    Now being able to store your data in the storage mode of your choosing is great, but what happens when you want to output the data to a browser or perhaps store it in a more permanent location? This is where the readout method comes to the rescue. The readout method makes our data storage system versatile, and it is what makes this class useful for so many tasks.                  

    function readout($mode,$file="",$headers=true,$del=":")
    {
         if($mode == "")
         $m = 0;
         else
         $m = $mode;
         switch($m)
         {
               case 0://an array
                    return $this->items;
                    break;
               case 1://a query string
                    return $this->to_query_string();
                    break;
               case 2://query string of id's
                    return $this->id_query();
                    break;
               case 3://an xslt result
                    return $this->from_xslt($file);
                    break;
               case 4://an xml formatted string
                    return $this->get_xml();
                    break;
               case 5://data dump into string
                    return print_r($this->items);
                    break;
               case 6://Excel compatibleCSV
                    return $this->make_csv($headers);
                    break;
               case 7://config file format
                    return $this->make_config($del);
                    break;
         }
    }

    This method acts as an interface to several methods designed to output stored data in a number of useful formats. The first argument is the mode, which is a number between 0 and 7. The other two we will look at in the next article.

    Each mode causes readout to return all the data stored in a different format. A mode of 0 will return the items array as is. A mode of 1 will return an array of query strings that include all the key/value pairs of each item in the items array. A mode of 2 will return an array of query strings that include only the unique Id for each item. This can make it much easier to build links to display different stored data on different pages.

    That's all I have room for in this part. In the next part, I will cover modes 3-7 (where things get really interesting), utility methods that can make your life much easier when using the datastore, go into output methods in depth, and provide you with a "snack for the road." See you next week!



     
     
    >>> More PHP Articles          >>> More By Chris Root
     

       

    PHP ARTICLES

    - 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
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





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