Welcome Guest, Not a member yet? Register   Sign In
Global preferences
#11

[eluser]jimsefton[/eluser]
[quote author="TheFuzzy0ne" date="1362667735"]I'd have a preferences model, which would run through each result and build an array in the format you expect them to be in, which would then be passed back to the controller, or even stored within the model itself for you to access.[/quote]

Yeah, from looking into this a bit further the issue is result_array() returns an array of arrays and that's why this syntax is not working.

Any chance of getting a peek at the model you use, as this is baffling me somewhat?

Thanks
#12

[eluser]TheFuzzy0ne[/eluser]
If you're only expecting a single result, you can use row_array(), but I wouldn't recommend hitting the database once for each preference.
#13

[eluser]jimsefton[/eluser]
[quote author="TheFuzzy0ne" date="1362668823"]If you're only expecting a single result, you can use row_array(), but I wouldn't recommend hitting the database once for each preference.[/quote]

What I am after is reading the preferences table and ending up with a load of global variables I can use in the app. I thought that's exactly what was discussed above but it appears it was never going to work.

It seems quite a straight forward thing to try and do but not quite sure why I am finding it so hard.
#14

[eluser]CroNiX[/eluser]
Just create a library that gets autoloaded. In the construct, get the preferences and store them in an array. Then just have a method to retrieve by the key. This allows you to run a single db call to retrieve all preferences so only 1 hit to DB.

Code:
class Prefs {
  private $pref_array = array();
  private $CI;

  public function __construct()
  {
    $this->CI =& get_instance();
    $this->load_prefs();
  }

  public function load_prefs()
  {
    //reset array so we can reload it if it was added to
    $this->pref_array = array();

    //retrieve all prefs from db
    $prefs = $this->CI->db->get('your_preference_table')->result_array();

    //put them into an easily accessible array according to KEY
    foreach($prefs as $pref)
    {
      $this->pref_array[$pref['key']] = $pref['value'];
    }
  }

  //get a preference by key from the preloaded array, or boolean FALSE if not exists
  function get_pref($key = FALSE)
  {
    return (isset($this->pref_array[$key])) ? $pref_array[$key] : FALSE;
  }
}

Then, if it's autoloaded, in any of your controllers you can just:

Code:
$pref = $this->prefs->get_key('some_key');
and check to see if it's boolean FALSE to make sure it existed.
#15

[eluser]CroNiX[/eluser]
You can also easily add a method to add a preference, which would store it in the db and recall load_prefs() method to reload it so it can be immediately used.
#16

[eluser]CroNiX[/eluser]
or another method to retrieve them all

Code:
public function get_all_prefs()
{
  return $this->pref_array();
}
#17

[eluser]jimsefton[/eluser]
Thanks for those tips, I am going to have a crack at it now, I'll let you know how I get on.

Cheers
#18

[eluser]jimsefton[/eluser]
[quote author="CroNiX" date="1362671980"]
Then, if it's autoloaded, in any of your controllers you can just:

Code:
$pref = $this->prefs->get_key('some_key');
and check to see if it's boolean FALSE to make sure it existed.[/quote]

Hi,

I presume you mean get_pref? If so I'm not sure if I'm doing something else wrong here?

Code:
Fatal error: Call to a member function get_pref() on a non-object in..

I presume the this is right (reflecting my DB column names)?:

Code:
$this->pref_array[$pref['setting_name']] = $pref['setting_value'];
#19

[eluser]jimsefton[/eluser]
Odd, it seems it is finding the library (if I rename the class it complains), but it is not finding the get_key (or get_pref, I have tried both) function. If I rename it something different I get the same message as above.
#20

[eluser]jimsefton[/eluser]
ok, looks like that was a typo... I have fixed it but now I get this:

Code:
Message: Undefined variable: pref_array




Theme © iAndrew 2016 - Forum software by © MyBB