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 $dates
array 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.
$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);
Have you tried just just building an array that you pass to the different views? I find $this->load->vars()
behaves unexpectedly.
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