Forum Moderators: coopster

Message Too Old, No Replies

PHP Recursive Queries

         

brotherhood of LAN

6:42 pm on Oct 13, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I had a script to make a directory, and it used recursive queries to ladder its way up the categories to the root directory for breadcrumb navigation.

The script wasn't really working for me, so I've built everything else that the script done, bar the recursive query bit! ;)

The script accesses a database, one for the categories, one for the links.

For the categories
CatID bigint(21) NOT NULL auto_increment,
CatName varchar(32) NOT NULL default '',
CatParent bigint(21) default NULL,
PRIMARY KEY (CatID),
UNIQUE KEY CatName (CatName)

The script works by gathering the value of the query string from a URL and assuming that as the "catid". If the "CatParent" is NOT NULL, then it is not a top category.

The recursive query would work by finding the parent category of the parent category and printing them all until it reaches NULL as the parent category.

The script seems to intermingle alot within itself and is hard for a newbie like me to fathom out ;) I can't see too much help on this sort of thing while searching for the title of the thread too.

The results of the query would be used for the title, and in a nice big H1 for breadcrumb navigation in the directory :) I can't imagine the code will be too difficult after looking at the other script, but if I spend any more time on this, then it won't be recursion- it'll be cursin' ;)

Any help would be appreciated

andreasfriedrich

1:30 am on Oct 14, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Shouldn´t something like this do the trick?

$path = array(); // store name and id for navigation 
if (!is_numeric($_GET['id'])) $_GET['id']=0;
$data = array('CatParent' => $_GET['id']);
for(;;) {
$sql = sprintf('SELECT CatParent, CatName FROM cats WHERE CatID=%s',
$data['CatParent']);
$query = mysql_query($sql);
if(!$query) break;
$data = mysql_fetch_assoc($query);
array_push($path, $data);
if($data['CatParent'] === NULL) break;
}

ukgimp

8:07 am on Oct 14, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



BoL

Have you looked at phpHoo.
Here is a tutorial based on phphoo
[webreference.com...]

I got it to work quite quickly. Working on the admin side of things at the moment. It is a good product (free also) and easily adaptable.

Cheers