Welcome to WebmasterWorld Guest from 54.159.165.175

Forum Moderators: coopster & jatar k

Create dynamic tree

dynamic tree

   
6:00 pm on Oct 26, 2012 (gmt 0)




$data = array(
array('id' => 1, 'pid' => 0, 'name' => 'node1', ...),
array('id' => 2, 'pid' => 0, 'name' => 'node2', ...),
array('id' => 3, 'pid' => 1, 'name' => 'node1.1', ...),
array('id' => 4, 'pid' => 3, 'name' => 'node1.1.1', ...)
...
);

OR

$data = array();
while($row = mysql_fetch_array(mysql_query('SELECT * FROM categories ORDER BY RAND()'), MYSQL_ASSOC)){
$data[] = $row;
}

// Create dynamic tree
function create($data){
foreach($data as &$v){
// Get childs
if(isset(self::$tree[$v['id']]))
$v['child'] =& self::$tree[$v['id']];


// push node into parent
self::$tree[$v['pid']][$v['id']] =& $v;


// push child into node
self::$tree[$v['id']]=& $v['child'];
}

// return Tree
return self::$tree[0];
}
2:27 am on Nov 1, 2012 (gmt 0)

WebmasterWorld Senior Member vincevincevince is a WebmasterWorld Top Contributor of All Time 10+ Year Member



That's a nice way to do it! Can you explain why you select the categories in RAND()om order?
 

Featured Threads

My Threads

Hot Threads This Week

Hot Threads This Month