我有一個工作區及其父標識表。每個父母可以有任意數量的等級。從多維PHP數組中將父節點的值串起來
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>
通過修改this solution我已經能夠把我的簡單列表轉換成多維數組並輸出值對,但沒有完整的路徑。
<?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
我在哪裏何去何從讓所有的家長工作場所到選擇的選擇嗎?
感謝馬克,這是完美的(我會更新我的問題,包括print_r($ result)調試我錯過了我的副本:) – Zoe 2013-03-18 05:00:55