Passing multiple variables to a view?
You need to access the variable in your view as you pass it. Using array($var1,$var2);
is valid but probably not what you wanted to achieve.
Try
$data = $var1 + $var2;
or
$data = array_merge($var1, $var2);
instead. See Views for detailed documentation how to access variables passed to a view.
The problem with using array($var1,$var2);
is that you will create two view variables: ${0}
and ${1}
(the array's keys become the variable names, you have two keys in your array: 0
and 1
).
Those variable names are invalid labels so you need to enclose the name with {}
on the view level.
By that you loose the ability to name the variables usefully.
The easiest thing to do in your controller:
$data['posts'] = $posts;$data['comments'] = $comments;$this->load->view('your_view', $data);
Then, in your view you just simply do something like:
foreach($posts as $post) {...}
You can hold any object in the $data variable that you pass to your view. When I need to traverse through result sets that I get from my models I do it that way.
Using associative array looks the best possible solution.take an array
$data = array( 'posts' => $posts, 'comments' => $comments,);$this->load->view('myview',$data);
Probably this will help you to get a solution.