Home arrow PHP arrow Page 4 - PHP Datastorage Class

Getting Data In - PHP

Many websites use databases for storing data for use by web applications. There are situations in which this is not the best solution, however. For light, moderate, or temporary storage requirements, you might prefer to use flat files, session variables, and cookies. This article will describe how to create a class that handle all of these. It is the first of two parts.

TABLE OF CONTENTS:
  1. PHP Datastorage Class
  2. Top of the Page
  3. Flat Files
  4. Getting Data In
  5. Getting Data Out
By: Chris Root
Rating: starstarstarstarstar / 10
November 14, 2005

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
 

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: