PHP
  Home arrow PHP arrow Page 4 - 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 In
    ( Page 4 of 5 )

    Our datastore class wouldn't be much good if we couldn't store anything in it, so that is the next thing to get into. The first method up is the add_new_item method.

    function add_new_item($arr,$id)
    {
         if(isarray($arr))
         {
               $keys = array_keys($arr);
               for($i= 0;$i < count($arr);$i++)
               {
                    $this->items[$id][$keys[$i]] = $arr[$keys[$i]];
                    $this->edited = true;
               }
         }
         else
         [
               $this->err("First argument must be an array",1);
         ]
    }

    This method accepts two arguments. The first is an associative array to serve as a row of records, and the second is a unique id to act as a key. It adds this array to the items array. If you are wondering why a loop is used, this is for future expansion of the method to allow processing of each element as it is set in the items array.

    One suggestion for using this method is for storing html form field values directly. By using the versatile array functions provided by PHP you could process $_POST or $_GET array values and then store them directly into datastore with very little code. This is especially useful for multi-page forms that are processed once all the data is collected.

    You also may have been wondering what the "err" method is. This is a method of datastore that will store the most recent error to allow easier debugging. The first argument is the error message and the second allows the method to both set and get error messages. If it is absent the method returns the current error.There is no error suppression in this datastore code, so once you have a good idea how to use it that may be something to include. You can then let the err method do the work of alerting you when something goes wrong.

    function err($msg="",$mode=0)
    {
         if(!$mode)
         {
               if($this->error != "")
               {
                    $ret = $this->error;
                    $this->error = "";
                    return $ret;
               }
               else
               {
                    return "No Error";
               }
         }
         else
         {
               $this->error = $msg;
         }
    }

    If you need to overwrite a specific item in the array or a specific field in an item you can use the set_item method.

    function set_item($id,$stuff,$field="")
    {
         if($this->is_item($id))
         {
               if($field == "")
               {
                    if(is_array($stuff))
                    {
                         $keys = array_keys($stuff);
                         for($i = 0;$i < count($stuff);$i++)
                         {
                               $this->items[$id][$keys[$i]] = $stuff[$keys[$i]];
                         }
                    }
                    else
                    {
                         $this->err("Third Argument Muat Be an Array",1);
                    }
               }
               else
               {
                    if(isset($this->items[$id][$field]))
                    {
                         $this->items[$id][$field] = $stuff;
                    }
                    else
                    {
                         $this->err("Item Not Found",1);
                    }
               }
         }
         else
         {
               return false;
         }
    }

    Arguments are the key of the item to alter, the data to store and an optional specific field to store the data in. If no field is specified then the second argument should be an associative array (data row) to replace the row specified. If a field is specified then the second argument should be the value you wish to store in that field.



     
     
    >>> 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 5 Hosted by Hostway
    Stay green...Green IT