I'm developing a website focusing on animals and have been struggling to figure out how to display the number of a taxon's grand-children, great grand-children, etc.
This script appears to do a good job displaying the number of children, except that it displays senseless numbers like 13 for species, which should have 0 children.
$stm = $pdo->prepare("SELECT Ch2.Taxon, COUNT(*) - 0 Children
FROM gz_life_mammals Ch
LEFT OUTER JOIN gz_life_mammals Ch2 on Ch.Taxon = Ch2.Taxon
WHERE Ch2.Parent = :MyURL
GROUP BY Ch.Parent");
$stm->execute(array(
'MyURL'=>$MyURL,
));
while ($row = $stm->fetch())
{
$ChildrenCount = $row['Children'];
}
echo $ChildrenCount;
I haven't been able to make it display the correct number of grand-children; the results are wildly irregular.
So I came up with another scheme. Below is some data from my database table, arranged in a parent-child relationship.
**Taxon | Parent | Rank**
Mammalia | Chordata | 25
Carnivora | Mammalia | 35
Canidae | Carnivora | 45
Canis | Canidae | 55
Canis-lupus | canis | 65
Suppose I navigated to the page MySite/life/carnivora, and I want to know how many species are in that order. That means I have to determine how many great grand-children Carnivora has.
If I can't do it the traditional way, then maybe I can make it work backwards. I simply write a query that counts every row where the value for the field Rank is '65' and whose great grandparent is 'Carnivora.'
Does anyone now how to write a query like that?