...

View Full Version : Site navigation using tree structure in database



Dylan Leblanc
01-07-2004, 07:25 PM
I was going to create the type of structure as outlined on these pages, but am wondering if anybody else is familiar with this concept, what it is called, and if there are any functions already written which I could make use of.

http://conf2.php.net/presentations/slides/phpperform/tree2.png

By using Left and Right fields in the database, along with the ID and Parent ID, it makes it simple to select a branch of the tree from leaf to root with just one query ,http://conf2.php.net/show/top7-vancouver/22/

Mainly, I think I am looking for a function which would arrange the Left and Right fields in the database in the proper numerical order.

http://conf2.php.net/show/top7-vancouver/21/
http://conf2.php.net/show/top7-vancouver/23/
http://conf2.php.net/show/top7-vancouver/24/

SDP2006
01-07-2004, 10:13 PM
Are you talking about how it shows up

CodingForums.com > :: Server side development > PHP > Site navigation using tree structure in database

Like that?

That is called Modified Preordered Tree Traversal.

It's a fairly simple concept. A great tutorial is here http://www.aesthetic-theory.com/showtut.php?page=tutorials&t=mptt

Dylan Leblanc
01-08-2004, 04:45 AM
Thanks, that'll do it

Dylan Leblanc
01-08-2004, 07:55 PM
Here is a function, and usage, I have written to order the preordered tree


<?

mysql_query('UPDATE SiteNav SET left = NULL, right = NULL');

echo (Circle());






// call with no parameter
// recursive function

function Circle($siteNavID = 1, $number = 1)
{

mysql_query('UPDATE DELAYED SiteNav SET `left` = '.$number.' WHERE siteNavID = '.$siteNavID);

$query = mysql_query('SELECT siteNavID FROM SiteNav WHERE parentID = '.$siteNavID.' ORDER BY name ASC');

if (mysql_num_rows($query) > 0) {
while ($row = mysql_fetch_object($query)) {
$number++;
$number = Circle($row->siteNavID, $number);
}
}

$number++;
mysql_query('UPDATE DELAYED SiteNav SET `right` = '.$number.' WHERE siteNavID = '.$siteNavID);

return ($number);


}

?>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum