I want to pass a variable from a controller to two view files.
public function post($id) {
$data['query'] = $this->blog_model->get_post($id);
$data['comments'] = $this->blog_model->get_post_comment($id);
$data['post_id'] = $id;
$data['total_comments'] = $this->blog_model->total_comments($id);
I want to pass the [total_comments] variable to the index.php and post.php views. How do I do that? Can I pass data to a view without loading them like this:
$this->load->view('post',$data);
Something along this lines?
The
'TRUE'argument tells CI to call your view and place it in a$data['post']variable. Later on you can use that variable in another view and just print it out.edit:
I’m not sure how you have organized your controllers and views but lets say something like this. This is just an example:
controller
Whenever you pass
$datato a controller you are passing the whole$dataarray to that view so you can access all of its elements in a view.For example in your comments.php view you will have
$comments,$someVariableand$sidebarvariables so you can do whatever you want with them.In comments.php you’d probably have something like this:
comments.php
Those same variables are available in the footer view, because you’ve passed $data to that view
I hope that this makes things a bit more clear to you.