arrays - Multi level menu with PHP -


i have subject table this:

id title parent_id full_path 

full_path finding parent recursive. this:

+----+-----------+-----------+-----------+ | id | title     | full_path | parent_id | +----+-----------+-----------+-----------+ | 40 | home      | 40        |         0 | | 41 | myhome1   | 41        |         0 | | 42 | ****      | 40-42     |        40 | | 43 | *****     | 41-43     |        41 | | 44 | ***       | 44        |         0 | | 45 | ****      | 45        |         0 | | 46 | *****     | 46        |         0 | | 49 | ******    | 49        |         0 | | 50 | **** **   | 40-42-50  |        42 | | 51 | **** **   | 40-42-51  |        42 | | 52 | **** **   | 40-42-52  |        42 | | 53 | *******   | 40-53     |        40 | | 54 | ****      | 40-54     |        40 | | 55 | ***       | 41-55     |        41 | | 56 | **** **** | 40-42-56  |        42 | | 57 | *******   | 44-57     |        44 | +----+-----------+-----------+-----------+ 

how can recursive array this:

array (     40 => array     (         42 => array         (             50,51,52,etc.         ),         53,         54     )     41 => array     (         43,         55,     ),     44 => array     (         57,     ),     etc... ) 

can use full_path create multilevel menu?

you use code below this. keep in mind works because subjects array small , recursion happens minimal. dont use approach on large arrays.

<?php $query = "select id, parent_id subjects"; //execute prefered method, eg mysqli  $rows = array(); while($row = $result->fetch_array(mysqli_assoc)) {   $rows[] = $row; }  function getchildren($p) {   global $rows;   $r = array();   foreach($rows $row) {     if ($row['parent_id']==$p) {       $r[$row['id']] = getchildren($row['id']);     }   }   return $r; }  $final = getchildren(0); ?> 

Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -