2008-09-30 82 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