• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Dataset from database accessible on all controllers and views

Hello thanks to the whole community and to whom you have dedicated your time to read this post.

The problem I have is that I have a table in the database called ´settings´ that inside has only two columns ´config_name´ and ´config_value´ with which I intend to control some functions of the application. The issue is that I capture all the data well but when I want to pass it globally so that all the drivers are able to read one or more given configurations, this does not happen, how can I do it? Greetings and thank you very much

I think that almost every developer had a similar problem. here is my solution for CI4.

PHP Code:
namespace Config;


SiteSettings extends BaseConfig
    // some persistent params
    public $dtf = [
        'short'     => 'd.m.y H:i'
    // params from DB
    public static $registrars = [

PHP Code:
namespace App\Models;

    public static function siteSettings() 
        $config = [];
= \Config\Database::connect();
        $res $db->query("SELECT config_name, config_value FROM settings");
foreach($res->getResult() as $row)
            $config[$row->config_name] = $row->config_value;

        return $config;

PHP Code:
    function configItem($name$default NULL)
        $cs config('SiteSettings');

        return $cs->{$name} ?? $default;

and further anywhere in the code

in controller: $param_test = configItem('param_test', 'some_default_value');
or in view: <?php configItem('param_test'); ?>
or in BaseController: $this->settings = config('SiteSettings');

at the same time, access to the database table occurs only once upon the first call of the config('SiteSettings')

Thank you very much for your input, something like this i have programmed, but i am using Codeigniter3 and I really can't find a way to make it work on CI3, Thanks

For CI3 the same thing can be done through the hook system

PHP Code:
class Site_settings_hook {
    function load() {
        $site_config = [];
        $ci = & get_instance();
$ci->db->query("SELECT config_name, config_value FROM settings");
if($qid && $qid->num_rows() > 0) {
            foreach($qid->result() as $row) {
                $site_config[$row->unit_name] = $row->unit_value;

            $config = & get_config();

PHP Code:
$hook['post_controller_constructor'][] = array(
    'class'    => 'Site_settings_hook',
    'function' => 'load',
    'filename' => 'Site_settings_hook.php',
    'filepath' => 'hooks'

as a result, all values from the database become available in the application config

Thumbs Up 
Thank you very much for the help, I will implement it and then I will tell you the result, again thank you very much and greetings from Cuba

Hello again, I have modified something in the code that you have put, because the CI super object did not work for me, in the end it was something like this:
PHP Code:
class Site_settings_hook extends CI_Model {

load() {


obtain_config() {
$settings = [];

$query $this->db->query('SELECT config_name, config_value FROM gallery_config');

        foreach (
$query->result() as $row){
$settings[$row->config_name] = $row->config_value;
$config = & get_config();

$config array_merge($config$settings);

but I still have the same problem, I have to declare each variable in each controller for each view I want to use, I would like to know if it is possible to do something like what I have developed outside of codeigniter for another project, which is something like this:
PHP Code:
    function obtain_config(){
$db = new db();
$config $_config = array();

$sql "SELECT config_name, config_value FROM settings";
$result $db->query($sql);

$row $result->fetch_array()){
$config[$row['config_name']] = $row['config_value'];


PHP Code:
$config obtain_config() 

so with this code when I want to get the value of a row in the confi_name table I just have to write.
PHP Code:
$config ['row name'
Thanks   Confused

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.