Welcome Guest, Not a member yet? Register   Sign In
Gathering database settings globally

How can I gather database information globally, instead of on every controller function?

I am working on a project that will have multiple themes. Each user will be able to choose which theme they want to use. Their choice will be recorded in my users MySQL table.

I don't really want to copy/paste the same SQL query 30 times throughout all of my controllers, so is there a way to just do it once and have the variables handy when I need them?


I create a library that gets autoloaded, and in its construct I would have it set all of that site specific stuff set up. If they select a theme, how are you tracking that? Session? Cookie? Profile in database? Just have your get_theme() method grab the value and get it however you need to and then return the correct theme, or default if none specified.

Then you would just set something like
$theme = $this->global_library->get_theme();
This is how most AUTH libraries work too. You autoload the library and it automatically checks to see if the user is logged in. If they are it lets them proceed, if not it redirects them to the login page, and then in all of your controller constructs you just have to do something like
instead of the actual query in every controller.

Ah, just what I needed.


Okay, please forgive my ignorance of PHP OOP.

How do I access my global objects from within my controller?

For example, my global library is something like
class Global_lib
    function Globals()
        $this->site_theme = 'theme_path';
        $this->user_theme = 'user selected theme';

How do I then access those two objects from my controller?

Well, I assume you are autoloading your library in your autoload.php config file.

If so, then in your controllers you just access it like:

or $some_param = $this->global_lib->function_that_returns_something($some_var);
or $some_param = $this->global_lib->some_public_var_of_lib_object;

I would write your library like:
<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

class Global_lib {

    private $CI;  //we will access CI from this var
    public $site_theme = 'theme_path';  //default value
    public $user_theme = 'user selected theme'; //default value
    public function __construct()
        $this->CI =& get_instance();        
        log_message('debug', 'Global_lib: Loaded');
    //some setters
    public function set_theme($theme)
        $this->site_theme = $theme;
    public function set_user_theme($theme)
        $this->user_theme = $theme;
    //a getter
    public function get_globals()
        return array('site_theme' => $this->site_theme, 'user_theme' => $this->user_theme);
    public function fetch_theme()
       //maybe fetch the theme for the current user from the database?
        $theme = $this->CI->db
          ->where('theme_name', $this->user_theme)
        return $theme;

Then, in your controller, you just
$settings = $this->global_lib->get_globals();

echo $settings['user_theme'];
// echos "user selected theme"

$this->global_lib->set_user_theme('new theme');
echo $this->global_lib->user_theme; //echos "new theme";

$data['theme'] = $this->global_lib->fetch_theme();

//load the view with our theme data
$this->load->view('admin/some_viewfile', $data);

Hope it gives you some ideas.

Excellent...that works great.

Thanks a lot.

Theme © iAndrew 2016 - Forum software by © MyBB