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!
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.
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With