Welcome Guest, Not a member yet? Register   Sign In
Loading preferences globally from a DB
#1

[eluser]Robert May[/eluser]
Hello there!

I'm trying to load configuration preferences from a table for the entire site, news items per page etc. I have the model for it set to autoload, but I'm unsure of how to actually access the preferences. I keep running in to a problem of CI sticking a stdClass array inside an array, and I can't figure out how to grab the data from it. This is no doubt due to me being a bit rusty with PHP, but any help on how to access it would be a huge help. I'm currently stuck with:
Code:
$preferences = $this->pref_model->get_prefs();
$this->load->library('pagination');
        $config['base_url'] = base_url().'index.php/news/index/';
        $config['total_rows'] = $this->db->count_all('news');
        $config['per_page'] = $preferences->pref_shortname->pref_value;
        $config['full_tag_open'] = '<p>';
        $config['full_tag_close'] = '</p>';

Which obviously doesn't work. How do I access the part of the array I want? Smile
#2

[eluser]xwero[/eluser]
You can iterate the result in the get_prefs model and create a one dimensional array
Code:
$return = array();
foreach($query->result_array() as $row)
{
   $return[key($row)] = $row;
}
#3

[eluser]Robert May[/eluser]
Thanks for the reply. It's sort of an improvement, but got some odd problems.

Code:
function get_prefs()
    {
        $this->db->select('*');
        $this->db->order_by('pref_id', 'desc');
        $query = $this->db->get('preferences');
        $return = array();
        foreach($query->result_array() as $row)
        {
           $return[key($row)] = $row;
        }
        return $return;
    }

It worked for a couple of minutes, but I added another pref to see if it fetched them all, and it seems to be making the script time out now. I've probably done something wrong, any ideas? Big Grin
#4

[eluser]Michael;[/eluser]
Greets Robert;

I recently did something similar in an application I am writing. I took a bit of a different route, but works quite nicely.

#1, Create yourself a library, I call mine "variables". You'll want to autoload it, and depending on your needs you'll have to figure out where the best place to load it is.

#2, The code:

Code:
&lt;?php if ( ! defined('BASEPATH')) exit('You are not permitted direct access to this file.');

class Variables {

    var $obj;

    function variables() {
        $this->obj =& get_instance();
        $this->obj->load->model('core/variables_model');
    }

    function getAll() {
        $variables = $this->obj->variables_model->getVariables();
        foreach ($variables as $variable){
            $this->config->set_item($variable->name, $variable->value);
        }
    }
}

From here you can season to taste.

Now, for my implementation I did not try to load CI config variables with it, so you might need to play around with things a bit, but this should not pose any problems for those variables at all.

I hope this helps you out, Good Luck.

Michael;




Theme © iAndrew 2016 - Forum software by © MyBB