Page 1 of 1
displaying search results in a non repeating way
Posted: Tue Mar 11, 2003 4:47 am
by jarow
PHP & MYSQL
I have a taxonomic database of animal species (phylum, class, order, family, species, etc) and would like to know if there is a way to display search results without repeating each category for each species. In other words, I want to avoid example 1 and achieve example 2. That is, if the name of the species changes but the prior categories stay the same I don´t want to repeat every category.
Example 1 NO!
Class Order Family Species 1
Class Order Family Species 2
Class Order Family Species 3
Class Order 2 Family 2 Species 4
Class Order 2 Family 2 Species 5
Class Order 2 Family 2 Species 6
Class Order 2 Family 3 Species 7
Class Order 2 Family 3 Species 8
Example 2 YES!
Class Order Family Species 1
Species 2
Species 3
Order 2 Family 2 Species 4
Species 5
Species 6
Family 3 Species 7
Species 8
Any ideas would be greatly appreciated.
James
Posted: Tue Mar 11, 2003 5:49 am
by pootergeist
Code: Select all
$base_class = '';
$base_order = '';
$base_family = '';
mysql_query......
while($row = mysql_fetch_array($query))
{
// assumes $row['class'] / $row['order'] etc
if($base_class !== $row['class'])
{
echo $row['class'].' ';
$base_class = $row['class'];
}
if($base_order !== $row['order'])
{
echo $row['order'].' ';
$base_order = $row['order'];
}
if($base_family !== $row['family'])
{
echo $row['family'].' ';
$base_family = $row['family'];
}
echo $row['species'].'<br />';
}
so basically you just set some new vars which mimic the current class/order/family - if the var is NOT the same as the row return, it is set to the same and echoed - if it is the same there is no change and no echo.
Posted: Tue Mar 11, 2003 5:51 am
by pootergeist
note:
you'd probably want to order your query results for best viewing
"SELECT ........ ORDER BY class, order, family, species";
Posted: Wed Mar 19, 2003 4:05 am
by jarow
Pootergeist,
a million thanks it works very well.
Do you have any ideas regarding the following. I am now trying to design the data display and want to do the following:
1. indent each category (without the periods) like:
phylum
.........class
...............order
......................family
.............................species
2.
If, for example, there is no family or order for a particular speciesI would want species to appear indented right under class as follows:
phylum
.........class
...............species
any ideas how I may do it?
Thanks again for your ideas
Jim
Posted: Wed Mar 19, 2003 4:52 am
by pootergeist
echo '<pre>';
all the scripting here - just add an else { echo '\t'; } // hard typed tabs (or '\t' ) or spaces (or dots or whatever) to each bit.
echo '</pre>';
the html <pre> tag preserves source formatting for page display - ie if you type a tab in the source, a tab will be shown on the displayed page.
Posted: Mon Mar 24, 2003 3:41 am
by jarow
Pootergeist,
I have discovered a glitch and don´t know why it occurring. I will try to explain it:
As you know there is a hierarchy that goes as follows
Phylum
Clase
Orden
Familia
Species
1) In one instance, I searched a phylum in which ORDER was empty and all that followed ORDER (i.e., familia, species) was not displayed
2) if there are various families in a phylum, the first species of the first family (in alphabetical order) never appears in the results.
If there are only two families in the phylum, the first family does not appear in the results.
3) If there is only one family and species for a phylum NO results are returned.
I hope I have made this clear and hope it´s not too much of a bother to take another look at this....I am such a beginner that I have no clue...
Many thanks
Jim
Below is the code you sent which I have modified:
$base_phylum = '';
$base_clase = '';
$base_orden = '';
$base_familia = '';
while($row_rsspecies = mysql_fetch_assoc($rsspecies))
{
$i=0; //used to now how much space you need.
// assumes $row_rsspecies['clase'] / $row_rsspecies['orden'] etc
if($row_rsspecies['phylum']) {
if($base_phylum != $row_rsspecies['phylum']) {
echo str_repeat("<dd>",$i++).$row_rsspecies['phylum'];
$base_phylum = $row_rsspecies['phylum'];
} else {
$i++;
}
} else {
$base_phylum = '';
}
if($row_rsspecies['clase']) {
if($base_clase != $row_rsspecies['clase']) {
echo str_repeat("<dd>",$i++).$row_rsspecies['clase'];
$base_clase = $row_rsspecies['clase'];
} else {
$i++;
}
} else {
$base_clase = '';
}
if($row_rsspecies['orden']) {
if($base_orden != $row_rsspecies['orden']) {
echo str_repeat("<dd>",$i++).$row_rsspecies['orden'];
$base_orden = $row_rsspecies['orden'];
} else {
$i++;
}
} else {
$base_orden = '';
}
if($row_rsspecies['familia']) {
if($base_familia != $row_rsspecies['familia']) {
echo str_repeat("<dd>",$i++).$row_rsspecies['familia'];
$base_familia = $row_rsspecies['familia'];
} else {
$i++;
}
} else {
$base_familia = '';
}
echo str_repeat("<dd>",$i++).$row_rsspecies['species'].'<br />';
}