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
https://forums.phpfreaks.com/topic/109020-solved-generating-phpmysql-select-menu/
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>';
?>

Note: http://www.phpfreaks.com/forums/index.php/topic,201057.0.html

 

This is related to my question, but I need to not only do this but to select a value from another field in the db and populate the pull-down with the info and have the first item be my value from that other field... make sense?

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.