Welcome Guest, Not a member yet? Register   Sign In
Dropdown Menu Help
#1

[eluser]roy3600[/eluser]
Hey guys,

Trying to create a simple drop down menu that populates via a table from my database. A simple "States" dropdown.

I have no problem doing this in normal PHP, here is my code:

Code:
<label for="state">State:</label>
  <select name="state" id="state">
  &lt;?php
  $state=mysql_query("SELECT * FROM states");
  while($rows=mysql_fetch_assoc($state)){
  ?&gt;
  <option value="&lt;?php echo $rows['state']; ?&gt;">&lt;?php echo $rows['state']; ?&gt;</option>
  &lt;?php }?&gt;
  </select>

However, I really want to learn Code Igniter and do this the correct way. I found on the User Guide this syntax for a dropdown:

$options = array(
'small' => 'Small Shirt',
'med' => 'Medium Shirt',
'large' => 'Large Shirt',
'xlarge' => 'Extra Large Shirt',
);

$shirts_on_sale = array('small', 'large');

echo form_dropdown('shirts', $options, 'large');

However, this does not show how to populate via a table, and I'm not to great at arrays and working with PHP While Loops inside of them.

Any help is appreciated, thanks!
#2

[eluser]roy3600[/eluser]
Did I explain this okay? I just need to know how to populate a dropdown menu with table information from a database.
#3

[eluser]LifeSteala[/eluser]
This is the code for what you need to do. As per the user guide you need to build an array of your options. From your array, you can generate the drop down menu, CI way.

Note: Code is untested.

Code:
$options = array();

// Loop through result set from the database
while($rows=mysql_fetch_assoc($state))
{
// Build key-value array
$options[$rows['state']] = $rows['state'];
}

// Display dropdown
echo form_dropdown('dropdown_name', $options);

Hope that helps.
#4

[eluser]roy3600[/eluser]
Worked, thanks!
#5

[eluser]PhilTem[/eluser]
[quote author="roy3600" date="1337798605"]Hey guys,
However, this does not show how to populate via a table, and I'm not to great at arrays and working with PHP While Loops inside of them.
[/quote]

Sure it doesn't show, how to populate the array needed for the dropdown because it (the user's guide as well as the CI developers) assume that you know how to populate an array with data from the database - just the way @LifeSteala stated above.




Theme © iAndrew 2016 - Forum software by © MyBB