我的數據庫結構看起來像
id|parent|
1 | 0 |
2 | 0 |
3 | 0 |
4 | 1 |
5 | 4 |
6 | 5 |
我需要的一個函數獲取一個id的父(即父= 0)作爲參數 對於例如.. get_parent(6)==返回1 我做了一些研究,並通過
$fnd=get_parent_id();
$array_reverse($fnd);
$parent_root=$fnd['0'];
發現這個問題
How can I recursively obtain the "parent ID" of rows in this MySQL table?
我試圖使這一功能
function get_parent_id($cid,$found=array())
{
array_push($found,$cid);
$sql="SELECT * FROM tbl_destinations WHERE id=$cid";
$result = mysql_query($sql) or die ($sql);
if(mysql_num_rows($result))
{
while($row = mysql_fetch_assoc($result))
{
$found[] = get_parent_id($row['parent'], $found);
}
}
return $found;
}
我打個電話,但我的方法是錯誤的。我哪裏做錯了?
你真的不應該*雙*後。你應該確保你收到你原來問題的答案。即使如此,也可以使用 –
。除非告訴我規則和規定,否則任何人都可以提供答案,我會很高興。對不起,我粗魯..我需要這個問題的答案拼命 –
請嘗試瞭解http://de.slideshare.net/billkarwin/models-for-hierarchical-data – Varon