質問
php / mysqlでフォームを構築しています。ロケーションとサブロケーションのリストを含むテーブルがあります。各サブロケーションには親ロケーションがあります。列「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ではスペースとダッシュのインデントを使用できます。ただし、再構築ループを構築する必要があります。次のようなもの:
<?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()
関数の使用が気に入らない場合は、&quot; display_order&quot;を追加できます。このテーブルの列を手動で設定し、 ORDER BY
に使用できます。
所属していません StackOverflow