I found a Modified Preorder Traversal Class here: http://www.aesthetic-theory.com/learn.php?mptt
Unfortunately, examples of the class weren't provided. I'm an OOP noob. Based on the class, could someone look at the class and tell me how to use it? I would really appreciate it. Thanks in advance!
//db structure
Code:CREATE TABLE `data` ( `id` MEDIUMINT UNSIGNED NOT NULL AUTO_INCREMENT, `title` VARCHAR(200) NOT NULL, `left_id` INT UNSIGNED NOT NULL, `right_id` INT UNSIGNED NOT NULL, `level` MEDIUMINT UNSIGNED NOT NULL, UNIQUE (`id`) );PHP Code:<?php
class Tree
{
var $data_array; // we'll store the data in this
var $cnx; // our connection to the database
// our constructer, we'll use it to connect to the database
function Tree()
{
$cnx = mysql_connect(DB_HOST, DB_USERNAME, DB_PASSWORD) or die ('Unable to connect to database');
mysql_select_db(DB_DATABASE, $cnx) or die ('Unable to select database');
}
/* this will display the data... we'll give it a few paramaters
* parent: this will be an integer or false,
* if its an integer we will get all children,
* if its false we will get the entire tree
* dig_levels: this will be an integer, or false
* represents the number of levels to dig BELOW PARENT for data.
* Examples, if its 3 and we get the entire tree, we'll get everything with a level <= 3
* (music,weezer,pinkerton BUT NOT elscorcho)
* ex: if its 2 and we have parent weezer (level:2) we'll get 2 + dig_level, or everything with a level <= 4
* inc_parent: whether or not to include parent in result set
*/
function get_data($parent = false, $dig_level = false, $include_parent = true)
{
if($parent)
{
$sql = 'SELECT `right_id`, `left_id`, `level` FROM ' . DATA_TABLE . ' ';
$where = 'WHERE `id`=' . $parent . ';';
$parent_res = mysql_query($sql . $where) or die(mysql_error());
unset($sql, $where);
if($include_parent) {
$lt = '<=';
$gt = '>=';
}
else
{
$lt = '<';
$gt = '>';
}
$parent_arr = mysql_fetch_array($parent_res);
//$parent_right = $parent_arr[0];
//$parent_left = $parent_arr[1];
//$parent_nest = $parent_arr[2];
$where = 'WHERE `left_id` ' . $gt . ' ' . $parent_arr[1] . ' AND `right_id` ' . $lt . ' ' . $parent_arr[0] . ' ';
unset($parent_res);
}
if($dig_level)
{
$nest_limit = isset($parent_arr) ? $parent_arr[2] + $dig_level : $dig_level;
$level_adjustment = $nest_limit - $dig_level; // we'll use this later
if(isset($where))
{
$where .= 'AND `level` <= ' . $nest_limit . ' ';
}
else
{
$where = 'WHERE `level` <= ' . $nest_limit . ' ';
}
}
$sql = 'SELECT * FROM `' . DATA_TABLE . '` ' . $where . 'ORDER BY `left_id` ASC';
unset($where, $parent_arr);
$raw_result = mysql_query($sql) or die(mysql_error());
while($item = mysql_fetch_array($raw_result)) {
$data[$item['id']]['title'] = $item['title'];
$data[$item['id']]['left_id'] = $item['left_id'];
$data[$item['id']]['right_id'] = $item['right_id'];
$data[$item['id']]['level'] = $item['level'];
$data[$item['id']]['relative_level'] = $item['level'] - $level_adjustment;
}
unset($raw_result);
$this->data_array = $data;
return $data;
}
}
?>




Bookmarks