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

[eluser]JamieBarton[/eluser]
Hi folks,

I have already my site almost built, with my own Auth system in place etc..

However, I have now a table called 'preferences' which contains the preferences for the website.

I now want to make use of the 'preferences' as I'm beginning to make the admin panel which will contain the prefs.

The table looks like:

ID / key / value

--

Now..

I want in MY_Controller to have a variable say 'pref' which I can get to from any controller and/or view.

Simply by for example...

Code:
$this->pref->get(key);


I know this is incredibly simple... I just can't seem to get my head around how to do it..

Should I involve a model, library or what here... and what things should be...

So far I have a library called... Site

I have written for example:

Code:
function get_pref ($key)
     {
         $this->db->where('site_key', $key);
         $query = $this->db->get($this->table);
         $x = $query->row();
         return $x->value;
     }

I know this is to go somewhere, but I'm totally just confusing myself.

The problem I'm having is because it's not just one row, it's multiple, and I'm grabbing by the key, not just one row.

Would appreciate any help! Smile
#2

[eluser]JamieBarton[/eluser]
In other words..

What I want to do in my controllers, or view is....

Code:
$this->pref->key;

Which will return the value, which is chosen by the key in the database..
#3

[eluser]thinkigniter[/eluser]
What I would do is...

See the post_controller_constructor under Hooks in the userguide.
http://ellislab.com/codeigniter/user-gui...hooks.html

and $GLOBALS["foo"] in php.net
http://php.net/manual/en/reserved.variables.globals.php

As well as..

if you want to make your eyes to bleed check out the new php5 ReflectionClass.
http://php.net/manual/en/reflectionclass...erties.php
#4

[eluser]kilishan[/eluser]
I'd think that creating a MY_Controller file to hold an Admin_Controller would be the easiest way. You can load up the preferences from the database and store them in the Admin_Controller as $this->prefs and you're set.

By using an Admin_Controller, this also gives you a single place you can authorize the user, set your admin template, etc, etc.

Then, for all of your admin-side controllers, just extend from the Admin_Controller class.

Phil Sturgeon has an excellent article about using base classes in CodeIgniter you might want to read through.
#5

[eluser]JamieBarton[/eluser]
Is there a way say if I in my controller grabbed all of the rows in the preferences table... put them into an array within the controller, i can then access through an array?

What would the code for that be..?
#6

[eluser]kilishan[/eluser]
[quote author="JamieBarton" date="1299472981"]Is there a way say if I in my controller grabbed all of the rows in the preferences table... put them into an array within the controller, i can then access through an array?

What would the code for that be..?[/quote]

Absolutely. It would be something like:

Code:
class Admin_Controller extends CI_Controller {

    protected $preferences;

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

    public function __construct()
    {
        parent::__construct();
        
        $query = $this->db->get('preferences');
        
        if ($query->num_rows())
        {
            $this->preferences = $query->result_array();
        }
    }
    
    //--------------------------------------------------------------------    

}

Then, a child class might find a preference by:

Code:
class Home extends Admin_Controller {

    public function index()
    {
        $my_pref = $this->preferences['my_pref'];
    }
    
    //--------------------------------------------------------------------
}

That should work, but it hasn't been tested. Smile I know the concept is sound and I do something similar quite often.

Hope that helps.
#7

[eluser]JamieBarton[/eluser]
Ok

That looks good, however,

In my database I have a table for peferences and then column names key and value.

I just want to return the value, depending on the key, which I'll call ->key for example...

How would I integrate that, as every row in the preferences table is a different 'preference'.

Thanks again Smile
#8

[eluser]diego6616[/eluser]
Code:
$my_pref = $this->preferences['my_pref'];

Since you have the entire table loaded on the result array, that should return the 'my_pref' value. 'my_pref' is an example, you should use the value of the "key" field you are looking for.
If you want to use the object-like syntax, you could return an object result (use result() instead of result_array()), an then use $this->preferences->my_pref . Beware that preferences names should be unique (for both cases).

You should also take a look at __get and __set magic methods (on the php.net site), but implement them on your own classes, I don't know if those are implemented on the CI core classes, and you could end up messing the core.
#9

[eluser]jimsefton[/eluser]
[quote author="diego6616" date="1299571616"]
Code:
$my_pref = $this->preferences['my_pref'];

Since you have the entire table loaded on the result array, that should return the 'my_pref' value. 'my_pref' is an example, you should use the value of the "key" field you are looking for.
If you want to use the object-like syntax, you could return an object result (use result() instead of result_array()), an then use $this->preferences->my_pref . Beware that preferences names should be unique (for both cases).

You should also take a look at __get and __set magic methods (on the php.net site), but implement them on your own classes, I don't know if those are implemented on the CI core classes, and you could end up messing the core.[/quote]

I realise this is quite an "old thread revival" but I am having a little trouble.

I use the code above and I get the following:

Code:
Message: Undefined index: site_name

(site_name is one of my setting values in setting_name column, which is the primary key)

if I use :

Code:
$my_pref = $this->preferences[0];

then it works, but I was under the impression that using the key value should work as described above.

Have I done something wrong?

#10

[eluser]TheFuzzy0ne[/eluser]
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.




Theme © iAndrew 2016 - Forum software by © MyBB