Welcome Guest, Not a member yet? Register   Sign In
Where to store a rarerly changing variable for the app?
#1

[eluser]iamzozo[/eluser]
Hi, im facing with a problem:

I would like to store a variable, which i use in different controllers.
This var is changed by the user optionally.
I would like it to be independent from routings to keep further developments flexible.
All users authenticated, who modify this var.

Could you give me an advice?

Thanks!
#2

[eluser]InsiteFX[/eluser]
Use a MY_Controller

Save as ./application/core/MY_Controller.php
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Base_Controller extends CI_Controller {

/**
  * -----------------------------------------------------------------------
  * Class variables - public, private, protected and static.
  * -----------------------------------------------------------------------
  */


  // ----------------------------------------------------------------------

/**
  *  __construct
  *
  * Class Constructor PHP 5+
  *
  * @access public
  * @return void
  */
  public function __construct()
  {
   parent::__construct();

}

  // ----------------------------------------------------------------------

/**
  * Index Page for this controller.
  *
  */
public function index()
{

}

}

// ------------------------------------------------------------------------
/* End of file Base_Controller.php */
/* Location: ./application/core/MY_Controller.php */

Now extend your other controllers from this Base_Controller

You can create static variables that will retain there value.
#3

[eluser]InsiteFX[/eluser]
A type of Registry Pattern class

Save as ./application/libraries/Property.php
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
* ------------------------------------------------------------------------
* Created by PhpDesigner7.
* User : Raymond L King Sr.
* Date : 1/26/2012
* Time : 9:44:31 PM
* The IgnitedCoder Development Team.
* ------------------------------------------------------------------------
* To change this template use File | Settings | File Templates.
* ------------------------------------------------------------------------
*/

class Property {

/**
  * array to hold static properties
  */
private static $properties = array();

// --------------------------------------------------------------------

/**
  * __construct()
  *
  * Constructor PHP 5+ NOTE: Not needed if not setting values!
  *
  * @access public
  * @return void
  */
public function __construct($params = array())
{
  if (count($params) > 0)
  {
   $this->properties = $params;
  }

  log_message('debug', "Property Class Initialized");
}

// --------------------------------------------------------------------

/**
  * set()
  *
  * sets a property key and value
  *
  * @access public
  * @param string - $key
  * @param string - $value
  * @return void
  */
public function set($key, $value)
{
  if ( ! isset($this->properties[$key]))
  {
   $this->properties[$key] = $value;
  }
}

// --------------------------------------------------------------------

/**
  * get()
  *
  * gets a property value by the key
  *
  * @access public
  * @param string - $key
  * @return mixed - returns a string property value else FALSE
  */
public function get($key)
{
  if (isset($this->properties[$key]))
  {
   return $this->properties[$key];
  }

  return FALSE;
}

// --------------------------------------------------------------------

/**
  * has()
  *
  * checks to see if the property has a key that exists
  *
  * @access public
  * @param string - $key
  * @return boolean - TRUE / FALSE
  */
public function has($key)
{
  return isset($this->properties[$key]);
}

// --------------------------------------------------------------------

/**
  * delete()
  *
  * deletes a property key
  *
  * @access public
  * @param string - $key
  * @return void
  */
public function delete($key)
{
  if (isset($this->properties[$key]))
  {
   unset($this->properties[$key]);
  }
}

// --------------------------------------------------------------------

/**
  * clear()
  *
  * clears and resets the properties array
  *
  * @access public
  * @return void
  */
public function clear()
{
  $properties = array();
}

}

// ------------------------------------------------------------------------
/* End of file Property.php */
/* Location: ./application/libraries/Property.php */

Either way you will need to save and restore the variable form your database.
#4

[eluser]iamzozo[/eluser]
Thanks a lot for yor help!




Theme © iAndrew 2016 - Forum software by © MyBB