简体   繁体   English

Code Igniter foreach和视图

[英]Code Igniter foreach and view

I am using a foreach loop in my controller 我在控制器中使用了foreach循环

foreach ($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 .. 它完成了工作..但是视图中的css,js元素被重复了..

please guide me how to make the css ,js to load only once :) 请指导我如何使css,js只加载一次:)

thanks 谢谢

It's because you are loading the view in the foreach statement. 这是因为要在foreach语句中加载视图。 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. 您需要在循环中一起收集$data ,然后在循环完成后调用视图。

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 从您的代码中,我相信您将在循环的每个迭代中重新分配$data变量

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. 在上面的示例中,加载视图后,$ data ['array']中将存在一个数组,因为您每次都将其覆盖。 If you are wanting $data['array'] to be a multi-deminsional array like you suggest, try this... 如果您希望$data['array']是您建议的多维度数组,请尝试以下方法...

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. 这将附加$data['array']而不是覆盖它,并且您将在整个循环中最终得到一个数组,该数组包含您附加的内容。

It's most likely the " do some stuff " that needs to be put in multi-dimensional array and not directly in the $data array. 最有可能需要将“ 做些事情 ”放入多维数组中,而不是直接放在$data数组中。 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>";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM