Variable storage class - any use to anyone? - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=22) +--- Thread: Variable storage class - any use to anyone? (/showthread.php?tid=25235) |
Variable storage class - any use to anyone? - El Forum - 12-05-2009 [eluser]markowe[/eluser] OK, bear in mind my complete CI newbieness, and fairly shaky PHP knowledge too. But I needed some code to store variables in between PHP sessions. For example, I have a cron job which has a lot of work to do importing some data and whatnot, so I can't do it all in one go, but rather I need to store where it got to last time it ran etc. This includes, say, what phase of the job I have got to (it works in some 4 phases), what the Last Modified date was of the file I am working with, what the last log message was, etc. etc. So... I figured, store all this in the database, and retrieve/store the values as and when I need them. So I need a simple dbase table, with two fields for each variable I want to store, called: 'var_name' and 'var_value' - here is a sample set of variable names and values, never mind what they represent, just an example: var_name var_value ------------------------------ failcount 1 lastmodified 1259930968 lastlog Trying to get new catalogue file on attempt 1. batchphase 1 newfilesize 1265057 currenttable event_tickets2 numrows 55000 dlfailcount 0 So I intialise my library with Code: $varParams = array('vartable' => 'variables'); The $varParams currently only contains the name of the table in the current database (passed as 'vartable' in the assoc. array), but I did it as an array figuring there might be more parameters to introduce at some later date. Then I can retrieve whatever variable I want with, say: Code: $failCount = $this->varstore->getValue('failcount'); ...and when I have finished, store all my values back with an associative array, e.g. Code: $this->varstore->storeValues (array('failcount' => '999', 'lastlog' => $logText)); Now, this is a dirt-simple piece of code, so please by all means pick holes in it, or tell me if there is some blindingly easier way of doing this, but Googling around, I was darned if I could find how you store variables persistently between PHP sessions (though I have a vague notion that later version of PHP do have some way of storing variables on disk). Probably there are possible bugs in this code too (I can see a couple of things already that I could have done better, like the select query), but it works great for me and might be useful for someone else too. Here is the code: Code: <?php if ( ! defined('BASEPATH')) exit('Get thee gone'); |