CodeIgniter Forums
Form Dropdown From Database Display Error - 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: Form Dropdown From Database Display Error (/showthread.php?tid=41396)



Form Dropdown From Database Display Error - El Forum - 05-06-2011

[eluser]jvk22[/eluser]
I'm using the form helper to populate a drop down, however when I put the database generated array in form_dropdown() it does not display correctly.

This is what the drop down looks like and the values are id and name
0
1
USA
1
2
U.K.

The actual id should be the value and the actual name should display

Current Source:
<select name="country">
<optgroup label="0">
<option value="id">2</option>

<option value="name">test</option>
</optgroup>
<optgroup label="1">
<option value="id">1</option>
<option value="name">United States</option>
</optgroup>
</select>

What I'm trying to achieve
<select name="country>
<option name="1>USA</option>
<option name="2">U.K.</option>
</select>


Form Dropdown From Database Display Error - El Forum - 05-08-2011

[eluser]toopay[/eluser]
Use foreach statement. And when you post code, please use code tags
Code:
// Remove any spaces
[ code]<--YourCodeHere--&gt;[ /code]



Form Dropdown From Database Display Error - El Forum - 05-09-2011

[eluser]jvk22[/eluser]
How exactly would I write the foreach though?


Form Dropdown From Database Display Error - El Forum - 05-09-2011

[eluser]danmontgomery[/eluser]
Code:
$dropdown = array();
foreach($db_result->result() as $row) {
    $dropdown[$row->id] = $row->name;
}

echo form_dropdown('country', $dropdown);

http://ellislab.com/codeigniter/user-guide/database/results.html