Welcome Guest, Not a member yet? Register   Sign In
Variable storage class - any use to anyone?
#1

[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');
$this->load->library('varstore', $varParams);

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');

class Varstore
{

var $appVars = array();
var $varValue;

    function Varstore ($params)
    {



    $this->varTable = $params['vartable'];
    $this->CI = & get_instance();
    $this->CI->load->database();

   $varSet = $this->CI->db->query('SELECT * FROM '. $this->varTable);

      foreach ($varSet->result_array() as $row)
      {
          $varName = $row['var_name'];
          $varValue = $row['var_value'];
          $this->appVars[$varName] = $varValue;

          

      }


    }

   function getValue ($varName)
   {
       $varValue = $this->appVars[$varName];
       return $varValue;
   }

   function storeValues ($vars)
   {
       foreach ($vars as $varName => $varValue)
       {

           $this->CI->db->where('var_name', $varName);
           $this->CI->db->update($this->varTable, array('var_value' => $varValue));
       }
   }

}

// end Varstore.php




Theme © iAndrew 2016 - Forum software by © MyBB