|
|
|
PHP Gurus: Need help with heirarchical function |
| message from David B on 22 Jul 2004 |
I'm working with an animals database and found a neat script that
apparently displays hierarchical data, beginning with whatever "node"
you choose. For example, I could choose "Carnivores," and it would
display the word Carnivora, followed by every family in the order
Carnivora, then every species in each family.
The tutorial is on this page:
http://www.sitepoint.com/article/hierarchical-data-database The script
looks amazingly simple - but I can't get it to work. I've tweaked it and
asked questions on a couple PHP forums with no success.
My table is named "fammammals," the children are in a field named
"FamMamName," and the parents are in a field named "FamMamParent."
My script displays nothing - not even any errors. When I tack "echo
$qry;" on the end, it displays this:
SELECT * FROM fammammals WHERE fammammals.IDMam = 'carfel'
Below is the entire script. Any tips?
Thanks.
<?php
// $node is the name of the node we want the path of
function get_path($Carnivora) {
// look up the parent of this node
$result = mysql_query("SELECT FamMamParent FROM fammammals
WHERE FamMamName='".$Carnivora."';");
$row = mysql_fetch_array($result);
// save the path in this array
$path = array();
// only continue if this $node isn't the root node
// (that's the node with no parent)
if ($row['FamMamParent']!='') {
// the last part of the path to $node, is the name
// of the parent of $node
$path[] = $row['FamMamParent'];
// we should add the path to the parent of this node
// to the path
$path = array_merge(get_path($row['FamMamParent']), $path);
}
// return the path
return $path;
}
echo $qry;
?>
|
| Gary White replied to David B on 22 Jul 2004 |
There is no wildcard character in the value you want to match. Use % for
multiple characters and _ for single characters. For example:
SELECT * FROM fammammals WHERE fammammals.IDMam = '%carfel%'
Gary
|
| David B replied to David B on 22 Jul 2004 |
Gary, did your response post correctly? I see your message header, but
there's nothing inside. I can read all the messages above and below yours.
I can't seem to reply to that message, either; I opened my original post
and hit "Reply" instead.
Thanks.
|
| Gary White replied to David B on 22 Jul 2004 |
I see it here alright. Here it is again:
David B wrote:
There is no wildcard character in the value you want to match. Use % for
multiple characters and _ for single characters. For example:
SELECT * FROM fammammals WHERE fammammals.IDMam = '%carfel%'
Gary
|
| David B replied to Gary White on 22 Jul 2004 |
On second thought, I may not know how to plug that in. The statement
"SELECT * FROM fammammals WHERE fammammals.IDMam = 'carfel' is actually
derived from a master query that's included on all my pages. Instead of
"carfel," it ends in ='$mycode'
Then I substitute a value for $mycode on each page. On the tiger page,
it might be this:
$mycode = 'carfel';
At any rate, I inserted % in both the master query and the $mycode on my
tiger page, like this:
MASTER QUERY...
SELECT * FROM fammammals WHERE fammammals.IDMam = '%$mycode%'
TIGER PAGE...
$mycode = '%carpan%';
When I preview my page, it now displays this:
SELECT * FROM fammammals WHERE fammammals.IDMam = '%carfel%' zzz SELECT
* FROM fammammals WHERE fammammals.IDMam = '%carfel%'
|
| Gary White replied to David B on 22 Jul 2004 |
If it's actually putting the zzz and a second copy of the SELECT in there,
then there is a logic error somewhere in your code. Don't have time to go
through all of it right now.
Gary
|
| David B replied to Gary White on 22 Jul 2004 |
Oops, sorry about the zzz. It's nothing - I just typed zzz on the page
to mark where the php function was supposed to display, then I forgot
about it.
|
| Gary White replied to David B on 22 Jul 2004 |
So what you're getting now is:
SELECT * FROM fammammals WHERE fammammals.IDMam = '%carfel%'
Before you were trying to use the LIKE predicate. Now, you're using =. You
cannot use wildcards with =. You should use wildcards with LIKE. I'm
guessing here, but what you want is:
SELECT * FROM fammammals WHERE fammammals.IDMam LIKE '%carfel%'
Gary
|
| David B replied to Gary White on 22 Jul 2004 |
Well, that answers a question I was asking myself the other day - What's
the difference between = and like?
It still didn't fix the script, but that's probably because my "master
query" is screwed up. I'll spend some time trying to figure out how to
condense it.
Thanks.
|
| Gary White replied to David B on 22 Jul 2004 |
If you want any further clarification:
LIKE "AB%" will match "ABC" but not "UAB"
LIKE "%AB" will match "UAB" but not "ABC"
LIKE "%AB% will match both "ABC" and "UAB" as well as "UABC"
= "AB" will only match "AB"
Good luck.
Gary
|
| David B replied to Michael Fesser on 23 Jul 2004 |
I learned a lot today, and I'm learning still more tonight! Thanks.
|
| David B replied to Gary White on 22 Jul 2004 |
OK, got it. Thanks.
|
| Gary White replied to David B on 22 Jul 2004 |
You're welcome.
Gary
|
|