简体   繁体   English

php foreach 语句的数组问题(代码点火器)

[英]php array problems with foreach statement (code-igniter)

Currently I have目前我有

private function getYears()
{
    return array('Test1', 'Test2', 'Test3', 'Test4');
}

in the index:在索引中:

$years = $this->getYears();

and in the view并且在视图中

<select>
    <?php foreach ($years as $row):?>
        <option><?=$row?></option>
    <?php endforeach;?>
</select>

The select box doesn't get filled? select 盒子没有装满?

How can I fix this?我怎样才能解决这个问题?

[edit] [编辑]

So far I noticed that the $years is empty and the getYears is not being called.. no idea why yet..到目前为止,我注意到 $years 是空的,并且 getYears 没有被调用..不知道为什么..

I don't know what you're iterating over.我不知道你在迭代什么。

You've passed the values to $years , yet you're iterating over $pages .您已将值传递给$years ,但您正在迭代$pages Is that a typo?那是错字吗?

In any case, you're iterating over an array of arrays.在任何情况下,您都在遍历 arrays 数组。 Do a var_dump on the $row variable and check what you're actually iterating over.$row变量执行 var_dump 并检查您实际迭代的内容。

<select>
    <?php foreach ($years as $row):?>
        <option><?=$row?></option>
    <?php endforeach;?>
</select>

Not advisable iteration, it is better to iterate in the controller and use view to create get html tags and just pass the data to the main view.不建议迭代,最好在 controller 中迭代并使用视图创建获取 html 标签并将数据传递到主视图。

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

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