2012-05-10 123 views
0

如何显示从我的mysql数据库中选择的下拉列表值。下拉列表取决于我的Category下拉列表。这些是代码:如何在php中显示从数据库中选择的下拉列表值

<?php $id = $_GET["id"]; 
if(!$pupcon){ 
    die(mysql_error()); 
} 
mysql_select_db($database_pupcon, $pupcon); 

$getDropdown2 = mysql_query("select * from tblitemname where CategoryID = $id"); 
    while($row = mysql_fetch_array($getDropdown2)){ 
     echo "<option value=\"".$row["ItemID"]."\">".$row["Item_Name"]."</option>"; 
    } ?> 

以下是第一个下拉列表(Category),其填充Item Name下拉代码。

<select name="Category" id="Category" class="field select large" onChange="loadXMLDoc(this.value);"> 
           <?php do { ?> 
           <option value="<?php echo $row_Recordset1['CategoryID']?>"<?php if (!(strcmp($row_Recordset1['CategoryID'], $row_editRecordset['CategoryID']))) {echo "selected=\"selected\"";} ?>><?php echo $row_Recordset1['CategoryName']?></option> 
           <?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); $rows = mysql_num_rows($Recordset1); if($rows > 0) { 
    mysql_data_seek($Recordset1, 0); 
    $row_Recordset1 = mysql_fetch_assoc($Recordset1);}?> 
          </select> 

回答

2

当你列出各种下拉选项,可以查看是否当前ItemID传递id值相匹配。如果它匹配,在那里扔一个selected="selected"。尝试:

$selected = ($row['ItemID'] == $id); 
echo "<option value=\"".$row["ItemID"]."\" ".($selected ? " selected=\"selected\"":"").">".$row["Item_Name"]."</option>"; 

编辑

我试图清理一些代码......不知道为什么您使用的是do...while因为$row_Recordset1不会是可在第一个迭代。

<select name="Category" id="Category" class="field select large" onChange="loadXMLDoc(this.value);"> 
    <?php 
    while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)) { 
    ?> 
    <option value="<?php echo $row_Recordset1['CategoryID']; ?>"<?php if (!(strcmp($row_Recordset1['CategoryID'], $row_editRecordset['CategoryID']))) { echo " selected=\"selected\""; } ?>> 
    <?php echo $row_Recordset1['CategoryName']; ?> 
    </option> 
    <?php 
    } 
    ?> 
</select> 
+0

下拉列表不显示任何内容。我编辑了我的问题,并包含了第一个下拉列表的代码。 –

+0

你忘了回声 –

+0

@RohitKumarChoudhary'echo'在哪里?我完全按照它的方式复制了他的代码,并将'$ id'更改为'$ row_Recordset1 ['ItemID']'。我试图创建一个编辑功能,我使用'Recordset1'来显示存储在我的数据库中的值 –

0

您可以使用此代码的内部,而

$selected=$row["ItemID"]==$id ?'Selected':''; 
echo "<option value=\"".$row["ItemID"]."\" {$selected} >".$row["Item_Name"]."</option>";; 
相关问题