![]() |
Gathering database settings globally - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: Gathering database settings globally (/showthread.php?tid=34483) |
Gathering database settings globally - El Forum - 09-30-2010 [eluser]CrazeD[/eluser] 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? Thanks. Gathering database settings globally - El Forum - 09-30-2010 [eluser]CroNiX[/eluser] 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 Code: $theme = $this->global_library->get_theme(); Code: $this->auth_library->check_login(); Gathering database settings globally - El Forum - 09-30-2010 [eluser]CrazeD[/eluser] Ah, just what I needed. Thanks Gathering database settings globally - El Forum - 09-30-2010 [eluser]CrazeD[/eluser] 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 Code: class Global_lib How do I then access those two objects from my controller? Gathering database settings globally - El Forum - 09-30-2010 [eluser]CroNiX[/eluser] 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: $this->global_lib->some_method(); 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: Code: <?php if (!defined('BASEPATH')) exit('No direct script access allowed'); Then, in your controller, you just Code: $settings = $this->global_lib->get_globals(); Hope it gives you some ideas. Gathering database settings globally - El Forum - 10-01-2010 [eluser]CrazeD[/eluser] Excellent...that works great. Thanks a lot. |