CodeIgniter Forums
Generating drop down menu from table - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Generating drop down menu from table (/showthread.php?tid=42459)



Generating drop down menu from table - El Forum - 06-08-2011

[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


Generating drop down menu from table - El Forum - 06-08-2011

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

should be

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



Generating drop down menu from table - El Forum - 06-08-2011

[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