2008-09-30 79 views
2

我正在用php/mysql構建表單。我有一張桌子,裏面有位置和次要位置的列表。每個子位置都有一個父級位置。 「parentid」列引用同一個表中的另一個locationid。我現在想將這些值加載到一個下拉以下列方式:嵌套下拉

--Location 1 
----Sublocation 1 
----Sublocation 2 
----Sublocation 3 
--Location 2 
----Sublocation 4 
----Sublocation 5 

等等,等等

任何人都得到了這樣一個優雅的解決方案?

回答

1

注意:這僅僅是僞代碼..我沒有嘗試運行它,但你應該能夠概念調整到你所需要的。

$parentsql = "SELECT parentid, parentname FROM table"; 

$result = mysql_query($parentsql); 
print "<select>"; 
while($row = mysql_fetch_assoc($result)){ 
    $childsql = "SELECT childID, childName from table where parentid=".$row["parentID"]; 
    $result2 = mysql_query($childsql); 
    print "<optgroup label=\".$row["parentname"]."\">"; 
    while($row2 = mysql_fetch_assoc($result)){ 
     print "<option value=\"".$row["childID"]."\">".$row["childName"]."</option>\n"; 
    } 
    print "</optgroup>"; 
} 
print "</select>"; 

隨着BaileyP的考慮有效的批評,這裏是如何做纔不至於在每一個循環中調用多個查詢的開銷:

$sql = "SELECT childId, childName, parentId, parentName FROM child LEFT JOIN parent ON child.parentId = parent.parentId ORDER BY parentID, childName"; 
$result = mysql_query($sql); 
$currentParent = ""; 

print "<select>"; 
while($row = mysql_fetch_assoc($result)){ 
    if($currentParent != $row["parentID"]){ 
     if($currentParent != ""){ 
      print "</optgroup>"; 
     } 
     print "<optgroup label=\".$row["parentName"]."\">"; 
     $currentParent = $row["parentName"]; 
    } 

    print "<option value=\"".$row["childID"]."\">".$row["childName"]."</option>\n"; 
} 
print "</optgroup>" 
print "</select>"; 
+0

我投下了這個,因爲執行任何循環的查詢 - 每次迭代是一個很大的禁忌。 – 2008-09-30 22:53:40

0

您可以在實際的HTML中使用空格/短劃線縮進。你需要一個recusrive循環來構建它。喜歡的東西:

<?php 

$data = array(
    'Location 1' => array(
     'Sublocation1', 
     'Sublocation2', 
     'Sublocation3' => array(
      'SubSublocation1', 
     ), 
    'Location2' 
); 

$output = '<select name="location">' . PHP_EOL; 

function build_items($input, $output) 
{ 
    if(is_array($input)) 
    { 
     $output .= '<optgroup>' . $key . '</optgroup>' . PHP_EOL; 
     foreach($input as $key => $value) 
     { 
      $output = build_items($value, $output); 
     } 
    } 
    else 
    { 
     $output .= '<option>' . $value . '</option>' . PHP_EOL; 
    } 

    return $output; 
} 

$output = build_items($data, $output); 

$output .= '</select>' . PHP_EOL; 

?> 

或類似的東西;)

0

理想情況下,你會在正確的順序選擇所有這些數據直接從數據庫中取出,然後循環輸出。這裏是我拿上你問

<?php 
/* 
Assuming data that looks like this 

locations 
+----+-----------+-------+ 
| id | parent_id | descr | 
+----+-----------+-------+ 
| 1 |  null | Foo | 
| 2 |  null | Bar | 
| 3 |   1 | Doe | 
| 4 |   2 | Rae | 
| 5 |   1 | Mi | 
| 6 |   2 | Fa | 
+----+-----------+-------+ 
*/ 

$result = mysql_query("SELECT id, parent_id, descr FROM locations order by coalesce(id, parent_id), descr"); 

echo "<select>"; 
while ($row = mysql_fetch_object($result)) 
{ 
    $optionName = htmlspecialchars((is_null($row->parent_id)) ? "--{$row->descr}" : "----{$row->desc}r", ENT_COMPAT, 'UTF-8'); 
    echo "<option value=\"{$row->id}\">$optionName</option>"; 
} 
echo "</select>"; 

如果你不喜歡使用coalesce()功能的東西,你可以添加一個「display_order」列此表,你可以手動設置,然後用爲ORDER BY