Location: PHPKode > scripts > MPTTA for PHP and MySQL > mptta-for-php-and-mysql/TEST_PHP4_MYSQL.php
<?php //5

// require the class
require 'PHP4_Mysql3MPTTA.class.php';

$db = mysql_connect( 'localhost', 'root', 'password' );
if( !@mysql_select_db( 'tree', $db ) ) {
	mysql_unbuffered_query( 'CREATE DATABASE tree', $db );
	mysql_select_db( 'tree', $db );
}

if( isSet( $_GET['deldb'] ) ) {
	$q = "DROP TABLE tree;
		DROP TABLE branch;
		CREATE TABLE tree (
			id INT(10) NOT NULL AUTO_INCREMENT,
			sx INT(10) UNSIGNED NOT NULL,
			dx INT(10) UNSIGNED NOT NULL,
			PRIMARY KEY (id)
		);
		CREATE TABLE branch (
			id INT(10) NOT NULL AUTO_INCREMENT,
			tree_id INT(10) UNSIGNED NOT NULL,
			name VARCHAR(255) NOT NULL,
			PRIMARY KEY (id)
		);
		INSERT INTO tree VALUES( NULL, 1, 16  );
		INSERT INTO tree VALUES( NULL, 2, 15  );
		INSERT INTO tree VALUES( NULL, 3, 6  );
		INSERT INTO tree VALUES( NULL, 4, 5  );
		INSERT INTO tree VALUES( NULL, 7, 8  );
		INSERT INTO tree VALUES( NULL, 9, 14  );
		INSERT INTO tree VALUES( NULL, 10, 11  );
		INSERT INTO tree VALUES( NULL, 12, 13  );
		INSERT INTO branch VALUES( NULL, 1, 'Massimiliano'  );
		INSERT INTO branch VALUES( NULL, 2, 'Francesco' );
		INSERT INTO branch VALUES( NULL, 3, 'Mario' );
		INSERT INTO branch VALUES( NULL, 4, 'Luigi' );
		INSERT INTO branch VALUES( NULL, 5, 'Fabio' );
		INSERT INTO branch VALUES( NULL, 6, 'Piero' );
		INSERT INTO branch VALUES( NULL, 7, 'Gianni' );
		INSERT INTO branch VALUES( NULL, 8, 'Luca' );
		";
	$rows = explode( ';', $q );
	for( $a = 0, $b = count($rows) - 1; $a < $b; $a++ ) {
		@mysql_unbuffered_query( trim( $rows[$a] ), $db );
	}
}

// CLASS DECLARATION
$MPTTA = &new PHP4_Mysql3MPTTA( $db );

// DB MANAGEMENT => ADD A NODE
if( isSet( $_GET['add'], $_GET['name'] ) ) {
	$MPTTA->addNode( $_GET['add'], $_GET['name'] );
}
// DB MANAGEMENT => REMOVE A NODE
elseif( isSet( $_GET['remove'] ) ) {
        $MPTTA->delNode( $_GET['remove'] );
}
// DB MANAGEMENT => MOVE A NODE
elseif( isSet( $_GET['move'], $_GET['where'] ) ) {
        $MPTTA->moveNode( $_GET['move'], $_GET['where'] );
}

// GET ALL NODES
$q1 = @mysql_query(
	"SELECT
		branch.id, branch.name, tree.sx, tree.dx 
	FROM
		branch, tree
	WHERE
		branch.tree_id = tree.id
	ORDER BY
		tree.sx
	ASC",
	$db
);
// THEN SHOW THOOSE
if( $q1 && mysql_num_rows( $q1 ) > 0 ) {
	$redirect = false;
	$output = "
	<script type=\"text/JavaScript\">
		function add( where ) {
			var w = window.prompt( 'Insert the name of the new branch' );
			var str = '{$_SERVER['PHP_SELF']}?add=' + where + '&name=' + w.split(\"'\").join(\"\\'\");
			window.open( str, '_self' );
		}
		function move( from ) {
			var w = window.prompt( 'Insert the name of the new branch id where to move' );
			var str = '{$_SERVER['PHP_SELF']}?move=' + from + '&where=' + w;
			window.open( str, '_self' );
		}
	</script>
	<pre>";
	$last_sx = 1;
	$tab = 0;
	
	while( $r = mysql_fetch_row( $q1 ) ) {
		$compare = (int)$r[2];
		if( $compare === ( $last_sx + 1 ) ) {
		    $tab++;
		}
		else if( $compare > ( $last_sx + 2 ) && $tab >= ( $compare - ( $last_sx + 2 ) ) ) {
		    $tab -= $compare - ( $last_sx + 2 );
		}
		$output .= str_repeat( "\t", $tab );
		if( $tab > 0 ) {
			$output .= "[<a title=\"add a branch\" href=\"#\" onclick=\"add( {$r[0]} ); return false;\">+</a>] {$r[0]} - <b><a title=\"remove this branch\" href=\"{$_SERVER['PHP_SELF']}?remove={$r[0]}\">{$r[1]}</a></b> ({$r[2]}, {$r[3]}) [<a title=\"move this branch\" href=\"#\" onclick=\"move( {$r[0]} ); return false;\">--></a>]<br />";
		}
		else {
			$output .= "[<a title=\"add a branch\" href=\"#\" onclick=\"add( {$r[0]} ); return false;\">+</a>] {$r[0]} - <b>{$r[1]}</b> ({$r[2]}, {$r[3]})<br />";
		}
		$last_sx = $compare;
	}
	echo "<a href=\"{$_SERVER['PHP_SELF']}?deldb=true\">re-create the database</a>".$output."</pre>";
}
else {
	echo "<a href=\"{$_SERVER['PHP_SELF']}?deldb=true\">create the database</a>";
}
// CLOSE DB CONNECTION
mysql_close( $db );
?>
Return current item: MPTTA for PHP and MySQL