Welcome Guest, Not a member yet? Register   Sign In
codeigniter 4 - common file <- database use
#1
Wink 
(This post was last modified: 05-26-2022, 12:31 PM by mefisteoo.)

Hi Smile
How can I use a database in a common file?
Common file (path: app/Config/Common.php)

I would like to get from the database saved page settings, i.e. page logo, language, menu...


Sebastian Smile
Reply
#2

Hello, can I please read this before any action.
Reply
#3

PHP Code:
<?php namespace Config;

use 
CodeIgniter\Config\BaseConfig;
use 
Config\Database;

class 
Common extends BaseConfig
{
    
    
public function __construct ()
    {
        parent::__construct();
        $db Database::connect();
        #SET GENERAL SETTINGS BEGIN
        $genSetsArr = array();
        $genSets $db->query('SELECT * FROM setting WHERE `setting_type`=1 ')->getResult();
        if (!empty($genSets)) {
            foreach ($genSets as $genSet) {
                $genSetsArr[strtoupper($genSet->name)] = $genSet->value;
            }
        }
        $this->general $genSetsArr;
        #SET GENERAL SETTINGS ENDS
        $db->close();
    }


//and you can get these data in a view using 
config('Common')->general['LOGO'
Reply




Theme © iAndrew 2016 - Forum software by © MyBB