Welcome Guest, Not a member yet? Register   Sign In
Generating drop down menu from table
#1

[eluser]digity[/eluser]
I'm trying to generate a drop down menu for a form with records from a table. The problem is I'm only getting the last record outputted to form_dropdown and not all of them.

The code:
Code:
...
// grab categories
    if ($category_data['rows'] = $this->topic_model->getCategory()) {

        $options = array();

        foreach($category_data['rows'] as $r) {
            $options = array($r->category_id => $r->category_name);
        }

    }

    echo form_dropdown('category_id', $options);
...

The output:
Code:
...
<select name="category_id"><option value="1">Uncategorized</option></select>
...


What am I doing wrong? How do I fix this?


Thanks in advance
#2

[eluser]danmontgomery[/eluser]
Code:
$options = array($r->category_id => $r->category_name);

should be

Code:
$options[$r->category_id] = $r->category_name;
#3

[eluser]digity[/eluser]
Spot on! Thanks, that worked perfectly! If you don't mind, can you tell me why I was wrong and why your code is correct? I don't know why it works and I want to learn




Theme © iAndrew 2016 - Forum software by © MyBB