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