我正在用php / mysql构建一个表单。我有一张包含位置和子位置列表的表格。每个子位置都有一个父位置。列“parentid”是指“parentid”。引用同一个表中的另一个locationid。我现在想以下列方式将这些值加载到下拉列表中:

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

等。等

任何人都有这样做的优雅解决方案吗?

有帮助吗?

解决方案

注意:这只是伪代码..虽然您应该能够根据需要调整概念,但我没有尝试运行它。

$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>";

其他提示

您是否在寻找 OPTGROUP 标签之类的内容?

optgroup绝对是最佳选择。它实际上是它的用途,

例如用法,查看 http://www.grandhall.eu/tips/submit的来源/ - “Grandhall Grill Used”下的选择器。

您可以在实际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;

?>

或类似的东西;)

理想情况下,您可以从数据库中选择正确顺序的所有这些数据,然后只需将其循环输出即可。这是我对你要求的内容的看法

<?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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top