homepage Welcome to WebmasterWorld Guest from 50.19.206.49
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Create dynamic tree
dynamic tree
mask_final




msg:4512739
 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];
}

 

vincevincevince




msg:4514470
 2:27 am on Nov 1, 2012 (gmt 0)

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

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved