hi im looking for a MPTT (modified pre-order tree transversal) script or a function class in php to help me retrieve a tree menu from mysql database. i was wondering if you can help me with the
wallpapers -animals -cats -lions -birds screensavers -fish -sharks -water -beaches -ocean categories -subcategory -subsubcategory
CREATE TABLE `mptt` (
`id` int(11) NOT NULL auto_increment,
`title` varchar(50) NOT NULL default '',
`left` int(11) NOT NULL default '0',
`right` int(11) NOT NULL default '0',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=10 ;
--
-- Dumping data for table `mptt`
--
INSERT INTO `mptt` VALUES (1, 'food', 1, 18);
INSERT INTO `mptt` VALUES (2, 'fruit', 2, 11);
INSERT INTO `mptt` VALUES (3, 'meat', 12, 17);
INSERT INTO `mptt` VALUES (4, 'red', 3, 6);
INSERT INTO `mptt` VALUES (5, 'yellow', 7, 10);
INSERT INTO `mptt` VALUES (6, 'beef', 13, 14);
INSERT INTO `mptt` VALUES (7, 'pork', 15, 16);
INSERT INTO `mptt` VALUES (8, 'cherry', 4, 5);
INSERT INTO `mptt` VALUES (9, 'banana', 8, 9);
<?
$db = mysql_connect("HOSTNAME", "USERNAME", "PASSWORD");
mysql_select_db("DATABASE_NAME",$db);
$sql = "SELECT title FROM mptt WHERE `left` BETWEEN 2 AND 11";
$result = mysql_query($sql ,$db);
if ($myrow = mysql_fetch_array($result)) {
do {
echo $myrow['title'].'<BR>';
} while ($myrow = mysql_fetch_array($result));
}else{
echo '<strong>ERROR: </strong>'.mysql_error();
}
?>
PHP / MYSQL Web Hosting at:<br>
<a href="http://www.webune.com"><img src="http://www.webune.com/images/headers/default_logo.jpg" border="0"></a><hr>
<?
$db = mysql_connect("HOSTNAME", "USERNAME", "PASSWORD");
mysql_select_db("DATABASE_NAME",$db);
echo '<hr>';
function display_mptt($root) {
global $db;
// retrieve the left and right value of the $root node
$sql2 = "SELECT * from mptt where id=$root";
$result2 = mysql_query($sql2 ,$db);
if(!$row2 = mysql_fetch_array($result2)) echo mysql_error();
echo '<h1>Indentation</h1>';
// start with an empty $right stack
$right = array();
// now, retrieve all descendants of the $root node
$sql = "SELECT * from mptt WHERE `left` BETWEEN ".$row2['left']." AND ".$row2['right']." ORDER BY 'left' ASC";
$result = mysql_query($sql ,$db);
// display each row
while ($row = mysql_fetch_array($result)) {
// only check stack if there is one
if (count($right)>0) {
// check if we should remove a node from the stack
while ($right[count($right)-1]<$row['right']) {
array_pop($right);
}
}
// display indented node title
echo str_repeat(' ',count($right)).$row['title']."<br>";
// add this node to the stack
$right[] = $row['right'];
}
}
display_mptt(1);
?>
Indentation
food
fruit
red
cherry
yellow
banana
meat
beef
pork
Joe Celko Nested Sets nested, nested sets
www.phpclasses.org/browse/package/1374.html
https://www.webune.com/forums/mptt-modified-pre-order-tree-transversal-php-tree-menu-script-t5713.html