dynamic dropdown

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
cyberpac9
Forum Newbie
Posts: 10
Joined: Thu Jun 16, 2005 8:08 am

dynamic dropdown

Post by cyberpac9 »

i have a form that is sorta like a registration page. i need a dropdown from which the user selects a building. here's the html version, straightforward:

Code: Select all

<select name=&quote;requiredbuilding&quote;>
<option value=&quote;1&quote;>Building 1</option>
<option value=&quote;2&quote;>Building 2</option>
<option value=&quote;3&quote;>Building 3</option>
</select>
however, we have all buildings stored in a database and it's easiest when a new building is added to just add it to the database and it will be automatically reflected in the dropdown. how do i accomplish this using php?

just a note: i am using php and mssql, not mysql...i may be able to translate mysql code to mssql, maybe :D. here's my building table structure:

Code: Select all

building_num | building_name
----------------------------
    1          building 1
    2          building 2
    3          building 3
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

loop over the results and put your <option>'s in the loop:

ex:

Code: Select all

echo "<select name=\"requiredbuilding\">";
while($row = mssql_fetch_assoc($result)){
  echo "<option value=\"".$row['id']."\">".$row['buildingname']."</option>";
}
echo "</select>";
User avatar
cyberpac9
Forum Newbie
Posts: 10
Joined: Thu Jun 16, 2005 8:08 am

Post by cyberpac9 »

let me ask you a stupid question:

when the user enters their info and submits the form two things happen. 1) their info is inserted into our database and part of that is the building_num which they chose. 2) their info is also emailed to an administrator. the admin doesn't want to know the building_num, but rather the building_name. so, when inserting into the database would i insert $row['building_num'] and email $row['building_name']?

thanks for your assistance....
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

well you're going to be using the post data that is sent from the form page so you can't use the $row[] array unless you perform another query.

you could easily do that on the action page by using the id that is passed from the form page in your select.

the other option would be to just pass both the name and the id in the value of your select separated by something to delimit the two (a colon, or comma etc). Then just explode the value on the other end to break it apart.

ex:

Code: Select all

&lt;option value=&quote;Redstone Building:4&quote;&gt;Redstone Building&lt;/option&gt;

Code: Select all

$buildinginfo = explode(&quote;:&quote;,$_POST&#1111;'requiredbuilding']);
echo $buildinginfo&#1111;0].&quote;&lt;-- name&quote;;
echo $buildinginfo&#1111;1].&quote;&lt;-- id&quote;;
User avatar
cyberpac9
Forum Newbie
Posts: 10
Joined: Thu Jun 16, 2005 8:08 am

Post by cyberpac9 »

thanks, i'll give that a try...
Post Reply