开发者

php array problems with foreach statement (code-igniter)

开发者 https://www.devze.com 2023-03-11 09:44 出处:网络
Currently I have private function getYears() { return array(\'Test1\', \'Test2\', \'Test3\', \'Test4\');

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 ?

How can I fix this ?

[edit]

So fa开发者_JAVA技巧r I noticed that the $years is empty and the getYears is not being called.. no idea why yet..


I don't know what you're iterating over.

You've passed the values to $years, yet you're iterating over $pages. Is that a typo?

In any case, you're iterating over an array of arrays. Do a var_dump on the $row variable and check what you're actually iterating over.


<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 .

0

精彩评论

暂无评论...
验证码 换一张
取 消