user2600101
user2600101

Reputation: 39

Codeigniter: How to access multidimensional arrays in config file with Controller?

Why is this not working? Please help me!

My Controller Code:

class frontend_controller extends CI_Controller {

    private $values;

    public function get_config() {

            parent::__construct(); 


        $CI = &get_instance(); //assigned the object to a variable
        $CI->config->load('example_config', TRUE); // get config file

        // get all config values
        $this->values = $CI->config->item('example_config',['frontend1']);

        $this->load->library('session');
        }



    }

the template render controller:

require_once('frontend_controller.php'); 
     class frontend1_controller extends frontend_controller {


        public function __construct() {
            parent::__construct();
        }


        function template() {



            $this->load->view('template', $this->get_config());
        }

     }

the Config file:

$config['frontend1']['base_url'] = "http://www.test.com/";
$config['frontend1']['login_error_url'] = "www.test.com/login_failed";
$config['frontend1']['login_success_url'] = "www.test.com/login_success";
$config['frontend1']['js'] = "www.test.com/js/file.js";
$config['frontend1']['css'] = "www.test.com/css/style.css";
$config['frontend1']['my_title'] = ' Test123' ;
$config['frontend2']['base_url'] = "www.test.com";
$config['frontend2']['login_error_url'] = "http://www.test.com/login_failed";
$config['frontend2']['login_success_url'] = "http://www.test.com/login_success";
$config['frontend2']['js'] = "http://www.test.com/js/file.js";
$config['frontend2']['css'] = "http://www.test.com/css/style.css";
$config['frontend2']['my_title'] = ' Test123' ;

in the template:

<p><?php echo $base_url ?></p>

I wanna read in the config file but only the frontend1-arrays. How do i do that?

Upvotes: 3

Views: 4440

Answers (3)

Config file:

$config['permissions'] = [
           [
              'name' => 'Xem',
              'key' => 'view',
              'val' => 0
           ],
           [
              'name' => 'Thêm',
              'key' => 'add',
              'val' => 0
           ]
        ];

Template file:

echo '<pre>';
print_r($this->config->item('permissions'));
echo '</pre>';
die;

And result:

Array(
  [0] => Array
    (
        [name] => View
        [key] => view
        [val] => 0
    )

   [1] => Array
    (
        [name] => Add
        [key] => add
        [val] => 0
    )
)

Upvotes: 0

MJ X
MJ X

Reputation: 9054

You can read all of them like this:

       $data = $this->config->item('frontend1');

       print_r($data); 

or you can read one by one like this:

$my_title = $this->config->item('my_title','frontend1');
echo $my_title;

Upvotes: 1

Bora
Bora

Reputation: 10717

Please write without brackets;

$this->values = $CI->config->item('example_config', 'frontend1');

Upvotes: 3

Related Questions