Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
292 views
in Technique[技术] by (71.8m points)

php - Populate select drop down from a database table

I have a table ("venues") that stores all the possible venues a volunteer can work, each volunteer is assigned to work one venue each.

I want to create a select drop down from the venues table.

Right now I can display the venue each volunteer is assigned, but I want it to display the drop down box, with the venue already selected in the list.

<form action="upd.php?id=7">
<select name="venue_id">
<?php //some sort of loop goes here
print '<option value="'.$row['venue_id'].'">'.$row['venue_name'].'</option>';
//end loop here ?>
</select>
<input type="submit" value="submit" name="submit">
</form>

For example, volunteer with the id of 7, is assigned to venue_id 4

<form action="upd.php?id=7">
<select name="venue_id">
    <option value="1">Bagpipe Competition</option>
    <option value="2">Band Assistance</option>
    <option value="3">Beer/Wine Pouring</option>
    <option value="4" selected>Brochure Distribution</option>
    <option value="5">Childrens Area</option>
    <option value="6">Cleanup</option>
    <option value="7">Cultural Center Display</option>
    <option value="8">Festival Merch</option>
</select>
<input type="submit" value="submit" name="submit">
</form>

Brochure Distribution option will already be selected when it displays the drop down list, because in the volunteers_2009 table, column venue_id is 4.

I know it will take a form of a for or while loop to pull the list of venues from the venues table

My query is:

$query = "SELECT volunteers_2009.id, volunteers_2009.comments, volunteers_2009.choice1, volunteers_2009.choice2, volunteers_2009.choice3, volunteers_2009.lname, volunteers_2009.fname, volunteers_2009.venue_id, venues.venue_name FROM volunteers_2009 AS volunteers_2009 LEFT OUTER JOIN venues ON (volunteers_2009.venue_id = venues.id) ORDER by $order $sort";

How do I populate the select drop down box with the venues (volunteers_2009.venue_id, venues.id) from the venues table and have it pre-select the venue in the list?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
$query = "SELECT volunteers_2009.id, volunteers_2009.comments, volunteers_2009.choice1, volunteers_2009.choice2, volunteers_2009.choice3, volunteers_2009.lname, volunteers_2009.fname, volunteers_2009.venue_id, venues.venue_name FROM volunteers_2009 AS volunteers_2009 LEFT OUTER JOIN venues ON (volunteers_2009.venue_id = venues.id) ORDER by $order $sort";

$res = mysql_query($query);
echo "<select name = 'venue'>";
while (($row = mysql_fetch_row($res)) != null)
{
    echo "<option value = '{$row['venue_id']}'";
    if ($selected_venue_id == $row['venue_id'])
        echo "selected = 'selected'";
    echo ">{$row['venue_name']}</option>";
}
echo "</select>";

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...