您好我有一个自引用的数据库表,基本上它包含所有物种和分类级别。所以例如我们在下面有一个海龟记录
tsn_id name rank_id parent_id
123 turtle 220 349
此海龟记录的父级是turtle_family 记录(在同一张表中),一直持续到 parent_id 为 0。0 表示动物王国(animalia parent_id 0)
tsn_id name rank_id parent_id
349 turtle_family 210 465
我想爬上每个物种记录的水平,比如乌龟。这是我的尝试
<?php
function get_each_species_record(){
$db = dbConnect();
$query= "select * from species_records where rank_id = 220"; //this rank_id is species
$result = $db -> Execute($query);
while($row=$result->FetchRow())
{
$tsn_id= $row['tsn'];
$complete_name = $row['complete_name'];
$parent_tsn = $row['parent_tsn'];
$rank = $row['rank_id'];
//* My objective *
//for each species
//take its parent tsn
//where tsn = parent tsn
//take that parents tsn
//where tsn = parent tsn
//till parent tsn = 0
climbtree($tsn_id);
}
}
//recursive function for each species
function climbtree($tsn_id){
if($parent_tsn == 0){
return 1; //quit and go onto next speices record?
}
else{
$queryone= 'select * from species_records where tsn = $parent_tsn';
$resultone = $db -> Execute($queryone);
return climbtree($tsn_id);
}
}
?>