Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Codeigniter: Constants in /application/config/constant.php from DB table

Tags:

codeigniter

is it possible to declare constants in /application/config/constant.php from a DB table?

I'm trying to do something like this.

$result = $this->db->select( 'attr, value' )->from( 'app_conf')->where( 'city', 53 )->or_where( 'city', -1 )->order_by( 'city' )->get();
$app_conf = $result->result_array();
// var_dump( $app_conf );
foreach( $app_conf as $row )
{
    define( "_{$row['attr']}", $row['value'] );
}

but right now I have to do it in every controller I create, so duplicating code and requiring to do changes all over if needed, which seems unnecessary!

like image 817
Anupam Avatar asked Dec 20 '22 19:12

Anupam


2 Answers

Extend the CI Controller and place it in the application/core directory as MY_Controller. In the extended controller's constructor place the above code. Now you can extend it from your controllers to execute the same code. So you are not required to write this code again and again.

like image 139
Nish Avatar answered Feb 22 '23 22:02

Nish


Make a new controller by extending the CI_Controller and use this controller as your main controller (use it to create new controllers instead of CI_Controller)

<?php 
class MY_Controller extends CI_Controller{
    function __construct()
    {
        parent::__construct();
        // Assuming that you have loaded all required library in autoload
        $result = $this->db->select( 'attr, value' )->from( 'app_conf')->where( 'city', 53 )->or_where( 'city', -1 )->order_by( 'city' )->get();
        $app_conf = $result->result_array();
        foreach( $app_conf as $row )
        {
            define( "_{$row['attr']}", $row['value'] );
        }
    }
}

create a new controller (a normal controller from MY_Controller instead of CI_Controller)

class Somecontroller extends MY_Controller {
    function __construct()
    {
        parent::__construct();
        // ...
    } 

    // other functions/mechods
 }
like image 36
The Alpha Avatar answered Feb 22 '23 23:02

The Alpha