简体   繁体   中英

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?

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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