I have a table that lists animal taxons (species, families, etc.) in a parent-child relationship. The name of each taxon, or scientific name, is in a field named Taxon, and each taxon’s parent is in a field named Parent. Consider the following data:
TAXON | PARENT
Animalia | Life
Chordata | Animalia
Mammalia | Chordata
Carnivora | Mammalia
Felidae | Carnivora
Panthera | Felidae
Panthera_leo | Panthera
If I’m on a page with the URL MySite/Life/Panthera_leo, I can easily display the parent (Panthera). But how can I display the grandparent (Felidae), great grandparent (Carnivora), etc.?
I figured out a relatively simply way of doing it. I simply replace the URL with the Parent in a script to get the grandparent, then plug the grandparent’s parent into a similar script to get its parent and so on. It looks like this:
$GP = mysql_fetch_assoc(mysql_query("SELECT Taxon, Parent, Rank
FROM gz_life L
WHERE Taxon LIKE '$Parent'"));
$Family = $GP['Parent'];
$GGP = mysql_fetch_assoc(mysql_query("SELECT Taxon, Parent, Rank
FROM gz_life L
WHERE Taxon LIKE '$Family'"));
$Order = $GGP['Parent'];
$GGGP = mysql_fetch_assoc(mysql_query("SELECT Taxon, Parent, Rank
FROM gz_life L
WHERE Taxon LIKE '$Order'"));
$Class = $GGGP['Parent'];
$GGGGP = mysql_fetch_assoc(mysql_query("SELECT Taxon, Parent, Rank
FROM gz_life L
WHERE Taxon LIKE '$Class'"));
$Phylum = $GGGGP['Parent'];
$Top = mysql_fetch_assoc(mysql_query("SELECT Taxon, Parent, Rank
FROM gz_life L
WHERE Taxon LIKE '$Phylum'"));
$Kingdom = $Top['Parent'];
The only problem is I have to write five separate queries to get from the species level to the animal kingdom. Actually, it doesn’t seem to be a problem; it works pretty fast. However, I wondered if might be able to pull the same values out of my navigation script? It displays “bread crumb” links that look something like this:
Animalia > Chordata > Mammalia > Carnivora > Felidae > Panthera > Panthera leo
This is the code:
function get_path($node, gz_life, Taxon) {
$result = mysql_query('SELECT Parent FROM gz_life WHERE Taxon="'.$node.'";');
$row = mysql_fetch_array($result);
$path = array();
if ($row['Parent']!='') {
$path[] = $row['Parent'];
$path = array_merge(get_path($row['Parent'], gz_life, Taxon), $path);
}
return $path;
}
$mypath = get_path($MyURL, gz_life, Taxon);
$MyLink = $mypath;
$MyLink = str_replace('Life', '', $MyLink);
$MyDisplay = $mypath;
for($i=0;$i<count($mypath);$i++){
$TopNav = "<a href=\\"".$MyLink[$i]."\\"> ".$MyDisplay[$i]."</a> >";
If I was on a species page, like MySite/Life/Panthera_leo, does anyone know how I could extrapolate the lion’s great great grandparent (order Carnivora) from the script above? In other words, I’d like to be able to echo $Order where it displays “Carnivora,” while $Class displays “Mammalia,” etc.
Thanks for any tips.