Jump to content

[SOLVED] generating php/MySQL select menu


webguync

Recommended Posts

Hello,

 

I currently have a static html dropdown menu coded as such

 

<table id="employees">

<tr>

<td>

<form action="">

<select name="employees">

<option value="employee1">Employee1</option>

<option value="employee2">Employee2</option>

<option value="employee3">Employee3</option>

<option value="employee4">Employee4</option>

</select>

</form>

</td>

</tr>

</table>

 

I want to change this to integrate with my MySQL table using PHP

 

my table is right now set up with two columns

 

ID  Employee_Name

 

1  John Smith

2  Bill Jones

3  Tom Turkey

4  Sam Iam

 

 

so that I can just swap out the table name and ID's whenever i want to generate a new menu. I know how to connect to the database and extract with the SQL, so I would just need help with the menu part to get the data from the MySQL table.

 

thanks in advance.

 

 

 

Link to comment
Share on other sites

Try something like:

 

<?php
//connect to db
$sql = "SELECT ID,Employee_Name FROM yourtable ORDER BY ID ASC";
$result = mysql_query($sql) or die(mysql_error());
echo '<select>';
while(list($id,$name) = mysql_fetch_row($result)){
    echo '<option value="'.$id.'" />'.$name.'</option>'."\n";
}
echo '</select>';
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.