2012-08-13 59 views
0

我正在尋找一些方法來根據mysql的結果選擇一個php組合框「<select>」。使用MySql在PHP中選擇Combobox值結果

其實我正在研究一個將用於編輯mysql表中現有值的php表單。我的第一個表格會簡單地傳遞記錄的ID進行編輯,這是這樣的< A HREF =「editCalendar.php?ID = 15」 >點擊編輯< /一>

守則editCalendar。 PHP是如下:

<?php 
    include("dbpath.php"); 
    $id = htmlspecialchars($_GET["id"]); 

    $sql="Select * from event_Date where eventid=" . $id; 
    $result=mysql_query($sql); 

    // Check result 
    // This shows the actual query sent to MySQL, and the error. Useful for debugging. 
    if (!$result) 
    { 
     $message = 'Invalid query: ' . mysql_error() . "\n"; 
     $message .= 'Whole query: ' . $query; 
     die($message); 
    } 

    while($row = mysql_fetch_assoc($result)) 
    { 
    $name=$row["EventTitle"]; 
    $close=$row["OpenOrClose"]; 
    $remarks=$row["Remarks"]; 
    $date=$row["EventDate"]; 
    $type=$row["Type"]; 
    } 
?> 

$close中得到的值將被用於選擇相同形式的「SelectClosedOrOpen」,即用戶將得到預先從所填充的列表中選擇選項。

<select id="SelectClosedOrOpen"> 
<option value="3">Select</option> 
<option value="0">Open</option> 
<option value="1">Closed</option> 
</select> 

意味着,如果$close爲0,則<option value="0">Open</option>必須選擇其他如$接近有1個,然後<option value="1">Closed</option>應該formload被自動選中。

+0

請停止使用不推薦使用的[mysql擴展](http://php.net/manual/en/faq.databases.php#faq.databases.mysql.deprecated)。切換到[mysqli](http://php.net/manual/en/book.mysqli.php)或[PDO](http://ch2.php.net/manual/en/book.pdo.php)。另請注意,您的代碼容易受到[SQL注入](http://en.wikipedia.org/wiki/SQL_injection)的影響。 – CodeZombie 2012-08-13 12:36:15

回答

2

你只需要在那裏寫selected屬性。試試這個

<select id="SelectClosedOrOpen"> 
<option value="3" <?php echo ($close == 3) ? 'selected="selected"': ''; ?>>Select</option> 
<option value="0" <?php echo ($close == 0) ? 'selected="selected"': ''; ?>>Open</option> 
<option value="1" <?php echo ($close == 1) ? 'selected="selected"': ''; ?>>Closed</option> 
</select> 
+0

解決了!謝謝,**亞倫W **。你真的很有幫助。 – Cyberpks 2012-08-13 12:31:58

+0

@ user1584140如果這有助於考慮將其標記爲答案。 http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work – 2012-08-13 14:31:12

0

幾年前我創建了這個函數。我希望它能幫助你。 它基本上需要您的選項數組和預先選擇的選項的值。它返回你選擇的選項,所以你還是要創建SELECT標籤,它允許您自定義的ID,JS等。

function drop_down_box_options_from_array($choices,$default="") 
{ 
     $output= ''; 
     // $choices is contructed using $choices[]=array("value","Displayed Choice"); 
     while (list ($key, $val) = each ($choices)) 
     { 
       $output.= '<option value="'; 
       $output.= $choices[$key][0]; 
       if ($default==$choices[$key][0]) 
       { 
         $output.= '" selected="selected" >'; 
       } 
       else 
       { 
         $output.= '">'; 
       } 
       $output.= $choices[$key][1]; 
       $output.= '</option>'; 
       $output.= "\n"; 
     } 
     return $output; 
} 

使用您的方案:

<select id="SelectClosedOrOpen" name="OpenOrClose"> 
<?php 
// defined here for clarity, but can be defined earlier ie in a config file 
$choices[]=array('3', 'Select'); 
$choices[]=array('0', 'Open'); 
$choices[]=array('1', 'Closed'); 
echo drop_down_box_options_from_array($choices, $row['OpenOrClose']); 
?> 
</select>