我有一张工作场所及其父 ID 的表格。每个父级可以有任意数量的级别。
id workplace parent
1 WCHN 0
2 Acute Services 1
3 Paediatric Medicine 2
4 Surgical Services 2
5 Nursing and Midwifery 1
6 Casual Pool 5
7 Clinical Practice 5
我需要创建一个单独的选择输入,列出工作场所及其所有父工作场所,有点像这样:
<select>
<option>WCHN > Acute Services > Paediatric Medicine</option>
<option>WCHN > Acute Services > Surgical Services</option>
<option>WCHN > Nursing and Midwifery > Casual Pool</option>
<option>WCHN > Nursing and Midwifery > Clinical Practice</option>
</select>
通过修改这个解决方案,我已经能够将我的平面列表变成一个多维数组并输出成对的值,但没有完整的路径。
<?php
public function list_all_workplaces()
{
$temp = $result = array();
$list = array();
// Get the data from the DB
$table = mysql_query("SELECT * FROM workplaces");
// Put it into one dimensional array with the row id as the index
while ($row = mysql_fetch_assoc($table)) {
$temp[$row['workplace_id']] = $row;
}
// Loop the 1D array and create the multi-dimensional array
for ($i = 1; isset($temp[$i]); $i++)
{
if ($temp[$i]['parent'] > 0)
{
$tmpstring = ($temp[$i]['workplace']); // workplace title
// This row has a parent
if (isset($temp[$temp[$i]['parent']])) {
$list[$i] = $temp[$temp[$i]['parent']]['workplace']." > ".$tmpstring;
//example output: Acute Services > Paediatric Medicine
// The parent row exists, add this row to the 'children' key of the parent
$temp[$temp[$i]['parent']]['children'][] =& $temp[$i];
} else {
// The parent row doesn't exist - handle that case here
// For the purposes of this example, we'll treat it as a root node
$result[] =& $temp[$i];
}
} else {
// This row is a root node
$result[] =& $temp[$i];
}
}
// unset the 1D array
unset($temp);
//Here is the result
print_r($result);
}
示例 print_r() 输出:
[1] => WCHN > Acute Services
[2] => Acute Services > Paediatric Medicine
我从这里到哪里去让所有父工作场所进入选择选项?