Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CodeIgniter $this->load->vars()

I'm wondering how $this->load->vars() works in CodeIgniter. The documentation is fairly vague about it.

I have the following code:

    $init = $this->init->set();

    $this->load->view('include/header', $init);
    $this->load->view('include/nav');

    $dates = $this->planner_model->create_date_list();
    $this->load->view('planner/dates_content', $dates);

    $detail = $this->planner_model->create_detail_list();
    $this->load->view('planner/detail_content', $detail);


    $this->load->view('include/footer');

However, I also need the $datesarray in my detail_content view. I was trying to load it with $this->load->vars() and hoping it would append to the $detail array, because the CI documentation states as follows:

You can have multiple calls to this function. The data get cached and merged into one array for conversion to variables.

Would it work if I do $detail['dates'] = $dates; ? Will it append the $dates array to $detail['dates'] then?

Thanks in advance.

like image 952
Joris Ooms Avatar asked Jan 19 '23 22:01

Joris Ooms


2 Answers

$this->load->vars() is perfect for this purpose. Try this:

$init = $this->init->set();// Won't be passed to the next 2 views
$this->load->view('include/header', $init);
$this->load->view('include/nav');

$dates = $this->planner_model->create_date_list();
$this->load->vars($dates);
$this->load->view('planner/dates_content');

$detail = $this->planner_model->create_detail_list();
$this->load->vars($detail);
$this->load->view('planner/detail_content');

What looks strange to me is that normally you pass an associative array as data, like $data['my_var_name'] = $var_value, so I assume your model calls are returning the data already structured with the variable names (array keys) that you'll use in your view which I do find odd, but then I know nothing of your application.

Here's a more "conventional" version:

$data['dates'] = $this->planner_model->create_date_list();
$this->load->view('planner/dates_content', $data);

$data['detail'] = $this->planner_model->create_detail_list();
// receives both dates and detail
$this->load->view('planner/detail_content', $data);
like image 131
Wesley Murch Avatar answered Jan 26 '23 00:01

Wesley Murch


Have you tried just just building an array that you pass to the different views? I find $this->load->vars() behaves unexpectedly.

like image 30
Dan Blows Avatar answered Jan 25 '23 23:01

Dan Blows