MPTT - Modified Preorder Tree Transversal Totorial

RegisterLogin
MPTT - Modified Preorder Tree Transversal Totorial
Post Description: MPTT - Modified Preorder Tree Transversal Totorial
Tags: MPTT, -, Modified, Preorder, Tree, Transversal, Totorial
This Post Was Posted On Apr 07, 2008 By web hosting #502
hi, my screen name is hostman. im with www.webune.com support.

i got started from this post: http://www.webune.com/forums/mptt-modified-pre-order-tree-transversal-php-tree-menu-script-t5713.html

to help our customer who have dedicated server we are creating this tutorial on how to create this type of algorithym.

Modified Preorder Tree Transversal Totorial

ok this is what i did

STEP 1: CREATE MYSQL DATABASE DUMP:

-- -- Table structure for table `mptt` --

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);

Leave Your Comments
Related Pages: [Add Your Website]
Post New Topic
esiaz
#4372 1
hi hostman, have you had the solution on edit and delete? can you kindly post it? i require them for my project. hope you can help. thanks
Aug 20, 2010 Reply Report abuse
hostman
#629 2

ADDING


now for the best part, we need to add, so we need to make a webform.



now save this file as mptt-add.php and run in your php website, click on the add link to add to each category. i followed the steps by Gijs Van Tulder



mptt-add.php
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);
if ($_GET['id']) {
if(isset($_REQUEST['Submit'])) {
$sql = "SELECT * FROM mptt where id=".$_GET['id'];
$result = mysql_query($sql ,$db);
$row = mysql_fetch_array($result);
$right = $row['right'];
// UPDATE RIGHT VALUES
$sql = "UPDATE mptt SET `right`=`right`+2 WHERE `right` > ". ($right - 1);
if(!$result = mysql_query($sql ,$db)) echo "$sql <br>".mysql_error();
// UPDATE LEFT VALUES
$sql = "UPDATE mptt SET `left`=`left`+2 WHERE `left` > ". ($right - 1);
if(!$result = mysql_query($sql ,$db)) echo mysql_error();
// INSERT NEW CATEGORY
$sql = "INSERT INTO mptt (`left`,`right`,`title`) values ('".$right."', '".($right +1)."', '".$_REQUEST['title']."')";
if(!$result = mysql_query($sql ,$db)) echo mysql_error();
} else {
?>
<form name="form1" method="post" action="<? echo $_SERVER['REQUEST_URI']; ?>">
Title: <input type="text" name="title">
<input type="submit" name="Submit" value="Submit">
</form>
<?
}
}
function display_mptt($root) {
global $db;
echo '<table border="0" width="200">';
// retrieve the left and right value of the $root node
$sql2 = "SELECT * from mptt where id=$root";
//$sql = "SELECT left,right FROM mptt WHERE `id`=1";
$result2 = mysql_query($sql2 ,$db);
if(!$row2 = mysql_fetch_array($result2)) echo mysql_error();
echo '<h1>Modified Preorder Tree Traversal</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 '<tr><td>'.str_repeat('&nbsp;&nbsp;',count($right)).$row['title'].'</td><td><a href="'.$_SERVER['PHP_SELF'].'?id='.$row['id'].'">[ add ]</a></td></tr>';
// add this node to the stack
$right[] = $row['right'];
}
echo '</table>';
}

display_mptt(1);
echo "<hr>";
?>
 
Apr 07, 2008 Reply Report abuse
hostman
#628 3

INDENTED ORDER DISPLAYING


ok, if you want indent the results so it will be a tree level, you can use this function:



STEP 3: Save this file at mptt-tree.php



mptt-tree.php

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('&nbsp;',count($right)).$row['title']."<br>";
// add this node to the stack
$right[] = $row['right'];
}
}

display_mptt(1);
?>




after you save this file and open it with your browser you will see this output:



Indentation

food fruit red cherry yellow banana meat beef pork


as you can see, it matches our table at the begining of this tutorial.
Apr 07, 2008 Reply Report abuse
hostman
#627 4

DISPLAYING

STEP 2: create php file called mptt.php with the following code:

mptt.php
<?
	$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();
  	}
  	
?>


NOTE:CHANGE THE FOLLOWING TO YOUR DB CONNECTION SETTINGS:

HOSTNAME (usually localhost) USERNAME (your database username) PASSWORD (username password) DATABASE_NAME (the database where you dump mysql above)

this file will display all file in the FRUIT category OUTPUT:
fruit red
yellow
cherry
banana
Apr 07, 2008 Reply Report abuse
Leave Your Comments...
©2011 Webune Forums - Mon Dec 19, 2011 11:03 am
Powered by: Webune Forums V3