-4

我有一个使用 PHP 构建的选项组。一切都很好,直到我不得不获取 DetailID 而不是 DetailName 的值来查找记录。我仍然需要显示 DetailName 但不需要显示 DetailID,我需要将 DetailID 添加到此。我该怎么做?

<?php
include("config.php");
$sql = "SELECT DetailType AS type, DetailID, GROUP_CONCAT(DISTINCT DetailName
          ORDER BY DetailName ASC   
          SEPARATOR '|') AS DetailName FROM tblDetails GROUP BY DetailType"; 
$result = mysql_query($sql) or die(mysql_error());  

while ($row = mysql_fetch_assoc($result)) { 
    echo "<optgroup label='{$row['type']}'>"; 
    $DetailNames = explode('|', $row['DetailName']); 
    foreach($DetailNames as $DetailName) { 
        echo '<option value='.$DetailID.'>'.$DetailName.'</option>';
    } 
    echo "</optgroup>";
}  
?>

表。tblLocDet LocationID 1,1,2,3 DetailID 1,2,3,4

tblDetails DetailType Feature, Meal Period, Service, Type DetailID 1,2,3,4 DetailName Buffet, Fireplace, WiFi, Waterfront

4

1 回答 1

2

这工作......

$sql = "SELECT DISTINCT DetailType AS type, DetailID, DetailName FROM tblDetails
            ORDER BY DetailType, DetailName";
                     $result = mysql_query($sql) or die(mysql_error());
                     $prev='';
                     while ($row = mysql_fetch_assoc($result)) { 
                     if ($prev != $row['type']) {
                         if ($prev) echo "</optgroup>";
                         echo "<optgroup label='{$row['type']}'>";
                         $prev = $row['type'];
                         }
                         echo "<option value='".$row['DetailID']."'>".$row['DetailName']."</option>";                            }
                         echo "</optgroup>";
于 2013-02-10T22:08:31.847 回答