2008-10-14 173 views
5

我有一張表(「場地」),用於存儲志願者可以工作的所有可能場地,每名志願者都分配給每個場地工作一個場地。從數據庫表中填充選擇下拉列表

我想從場地表中創建一個選擇下拉列表。

現在我可以顯示每個志願者分配的場地,但我希望它顯示下拉框,列表中已經選擇了場地。

<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> 

例如,具有7的ID志願者,被分配給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. 

我知道它將採取的形式爲或while循環拉從場地的列表場館表

我的查詢是:

$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"; 

如何填充選擇下拉框機智h場館表(volunteers_2009.venue_id,venues.id)從場館列表中選擇,並預先選擇列表中的場地?

回答

17
$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>"; 

:)

4

假設你有場地的數組...我個人不喜歡混合與其他巫術的SQL。

function displayDropDown($items, $name, $label, $default='') { 
    if (count($items)) { 
    echo '<select name="' . $name . '">'; 
    echo '<option value="">' . $label . '</option>'; 
    echo '<option value="">----------</option>'; 
    foreach($items as $item) { 
     $selected = ($item['id'] == $default) ? ' selected="selected" : ''; 
     echo <option value="' . $item['id'] . '"' . $selected . '>' . $item['name'] . '</option>'; 
    } 
    echo '</select>'; 
    } else { 
    echo 'There are no venues'; 
    } 
} 
+0

我有一個表,存儲所有的場館(ID,venue_name) 的venues.id存儲在volunteers_2009表(場地)(volunteers_2009.venue_id) – Brad 2008-10-14 02:02:00

2
 <?php 
     $query = "SELECT * from blogcategory"; 
     //$res = mysql_query($query); 
     $rows = $db->query($query); 
     echo "<select name = 'venue'>"; 
     // while (($row = mysql_fetch_row($res)) != null) 
     while ($record = $db->fetch_array($rows)) 
     { 
      echo "<option value = '{$record['CategoryId']}'"; 
      if ($CategoryId == $record['CategoryId']) 
       echo "selected = 'selected'"; 
      echo ">{$record['CategoryName']}</option>"; 
     } 
     echo "</select>"; 
     ?> 
0
<!DOCTYPE html> 
<html> 
<head> 
    <title>table binding</title> 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> 

</head> 
<body> 
    <div id="mydiv" style="width:100px;height:100px;background-color:yellow"> 

     <select id="myselect"></select> 
    </div> 

</body> 
</html> 


<?php 
include('dbconnection.php'); 

$sql = "SHOW TABLES FROM $dbname"; 
$result = mysqli_query($conn,$sql); 

if (!$result) { 
    echo "DB Error, could not list tables\n"; 
    echo 'MySQL Error: ' . mysqli_error(); 
    exit; 
} 

while ($row = mysqli_fetch_row($result)) { 
    echo "<script> 
    var z = document.createElement('option'); 
    z.setAttribute('value', '".$row[0]."'); 
    var t = document.createTextNode('".$row[0]."'); 
    z.appendChild(t); 
    document.getElementById('myselect').appendChild(z);</script>"; 

} 



?> 
相關問題