I am using a foreach loop in my controller
fore开发者_如何学JAVAach ($x as $y) {
// do some stuff
// load view
$this->load->view('success', $data);
// $data can be multiple arrays
}
it does the work..but the css, js , elements in the view gets repeated ..
please guide me how to make the css ,js to load only once :)
thanks
It's most likely the "do some stuff" that needs to be put in multi-dimensional array and not directly in the $data
array. It's better to post your "do some stuff" and your view, but anyway, this is how you should do it:
$data_holder = array();
foreach ($x as $y) {
// do some stuff
$data_holder[] = $do_some_stuff_results;
}
$data['do_stuff_array'] = $data_holder;
$this->load->view('success', $data);
Now you can loop that variable in your view and not only get one result, maybe something like:
foreach ($do_stuff_array as $arr)
echo "<li><img src=\"" . $arr['img_src'] . "\" alt=\"" . $arr['img_title'] . "\" /></li>";
It's because you are loading the view in the foreach statement. Every time the loop goes around, the view gets loaded again. You need to collect your $data
together in the loop and then call the view after the loop has finished.
foreach ($x as $y) {
// do some stuff
// $data can be multiple arrays
}
//load the view after the foreach has finished
$this->load->view('success', $data);
You currently are loading the view repeatedly at each iteration of the loop. Like this, it loads it once the loop has finished.
Second idea
From your code, I believe you are re-assigning your $data
variable each iteration of the loop
foreach ($x as $y) {
// do some stuff
//
$data['array'] = array("here","is","an","array");
}
//load the view after the foreach has finished
$this->load->view('success', $data);
In the above example once the view is loaded there will be one array in $data['array'] because you are overwriting it every time. If you are wanting $data['array']
to be a multi-deminsional array like you suggest, try this...
foreach ($x as $y) {
// do some stuff
//
$data['array'][] = array("here","is","an","array");
}
//load the view after the foreach has finished
$this->load->view('success', $data);
This will append $data['array']
instead of overwriting it, and you'll end up with an array of whatever you append to it throughout the loop.
精彩评论