Jump to content


Photo

help with displaying menu hierarchy


Best Answer mac_gyver, 24 April 2013 - 06:30 AM

you would use recursion to process any arbitrary number of parent/child levels.

 

retrieve all the data into an array with the main index being the parent's id. the order of the data at any parent's id level would be the order that is it displayed as (see main index 0 and 4 entries in the following example.)

// makeup some data (your code would get the data from wherever it is stored at)
// id=>x is each item's id. menu=>'string' would be your menu display text
$menu = array();
$menu[0][] = array('id'=>2,'menu'=>'main menu, w/children');
$menu[0][] = array('id'=>1,'menu'=>'main menu, no children');
$menu[2][] = array('id'=>3,'menu'=>'sub-menu');
$menu[4][] = array('id'=>5,'menu'=>'sub-menu1');
$menu[4][] = array('id'=>8,'menu'=>'sub-menu2');
$menu[0][] = array('id'=>4,'menu'=>'main menu, w/children w/children');
$menu[5][] = array('id'=>6,'menu'=>'sub-sub-menu');
$menu[6][] = array('id'=>7,'menu'=>'sub-sub-sub-menu');

function recursive_display(&$arr, $parent, $indent=0){ // if some of this function code looks familiar to anyone here, it was found here on phpfreaks
    $ind = $indent * 30;
    if (isset($arr[$parent]))
        foreach($arr[$parent] as $rec){
            echo "<div style='width:500px; margin-top:5px; margin-left: {$ind}px; padding:5px; border:1px solid gray;'>
            ID: {$rec['id']} - " . (($parent) ? "Sub-menu for: $parent - " : '') . "Produce link here for: {$rec['menu']}
            </div>";
            recursive_display($arr, $rec['id'], $indent+1);
        }
}

// call the recursive display function
recursive_display($menu, 0);
Go to the full post


  • Please log in to reply
8 replies to this topic

#1 lordrt

lordrt

    Advanced Member

  • Members
  • PipPipPip
  • 30 posts

Posted 24 April 2013 - 05:02 AM

Hello, I need some help in re-writing a code concerning the display of menus, where I will require to show several levels in the menu hierarchy. So far, I have a code, which was written long ago by another colleague (see attachment), where it can retrieve the "primary" menus, along with the children if any, but only upto one level, e.g. Parent Menu -> Child Menu

 

However I would like to make the code retrieve all the levels, irrespective of the number, and show them, for e.g. Parent Menu -> Child Menu -> Sub-child Menu -> Sub-sub Child Menu and so on.

 

Anyone can help me pls?

 

Attached File  menus.php   5.34KB   3 downloads
 



#2 mac_gyver

mac_gyver

    Advanced Member

  • Moderators
  • 1,872 posts

Posted 24 April 2013 - 06:30 AM   Best Answer

you would use recursion to process any arbitrary number of parent/child levels.

 

retrieve all the data into an array with the main index being the parent's id. the order of the data at any parent's id level would be the order that is it displayed as (see main index 0 and 4 entries in the following example.)

// makeup some data (your code would get the data from wherever it is stored at)
// id=>x is each item's id. menu=>'string' would be your menu display text
$menu = array();
$menu[0][] = array('id'=>2,'menu'=>'main menu, w/children');
$menu[0][] = array('id'=>1,'menu'=>'main menu, no children');
$menu[2][] = array('id'=>3,'menu'=>'sub-menu');
$menu[4][] = array('id'=>5,'menu'=>'sub-menu1');
$menu[4][] = array('id'=>8,'menu'=>'sub-menu2');
$menu[0][] = array('id'=>4,'menu'=>'main menu, w/children w/children');
$menu[5][] = array('id'=>6,'menu'=>'sub-sub-menu');
$menu[6][] = array('id'=>7,'menu'=>'sub-sub-sub-menu');

function recursive_display(&$arr, $parent, $indent=0){ // if some of this function code looks familiar to anyone here, it was found here on phpfreaks
    $ind = $indent * 30;
    if (isset($arr[$parent]))
        foreach($arr[$parent] as $rec){
            echo "<div style='width:500px; margin-top:5px; margin-left: {$ind}px; padding:5px; border:1px solid gray;'>
            ID: {$rec['id']} - " . (($parent) ? "Sub-menu for: $parent - " : '') . "Produce link here for: {$rec['menu']}
            </div>";
            recursive_display($arr, $rec['id'], $indent+1);
        }
}

// call the recursive display function
recursive_display($menu, 0);

multi-purpose programming fool and resident naysayer [We try not be negative in replies, but telling someone what they're doing wrong, while staying politically correct, isn't always going to happen.]

#3 jugesh

jugesh

    Advanced Member

  • Members
  • PipPipPip
  • 65 posts

Posted 24 April 2013 - 07:56 AM

I have written two functions to generate parent child relationship in menus

 

 

//function to get the category list of a table when calling the function initially the 1st parameter will be 0(zero).
function getCategory($catid, $tbl, $main_field_id="CatID", $parent_field_id="ParentID", $main_field_name="CatName", $separator=" --", $merge_with_db_table = false, $db_fields="*", $sql_cond="", $top_parent_id=0, $order_by_field=""){
    //echo $separator; exit;
    global $cat_arr, $objDB,$level;
    $level = 0;
    if(!empty($db_fields))
        $sql = "SELECT ".$db_fields." FROM `".$tbl."` WHERE `$parent_field_id`=".$catid." AND 1=1 ".$sql_cond;
    if(!empty($order_by_field))
        $sql .= " ORDER BY `".$order_by_field."`";
    
    //echo $sql . "<br>";
        
    $res = $objDB->ddlQuery($sql);
    while($row=$objDB->get_row($res, 'MYSQL_ASSOC'))
    {
        $l = getLevel($row[$main_field_id], $tbl, $main_field_id, $parent_field_id, $main_field_name, $top_parent_id);
        $level=0;
        $append_string='';
        for($i=1;$i<=$l;$i++){
            $append_string.=$separator;
        }
        $append_string.=" ";
        
        $arr_temp = array("$main_field_id"=>$row[$main_field_id],"$main_field_name"=>$append_string.$row[$main_field_name],"level"=>$l);
        if($merge_with_db_table)
            $cat_arr[] = array_merge($row, $arr_temp);
        else
            $cat_arr[] = $arr_temp;
        
        getCategory($row[$main_field_id],$tbl,$main_field_id,$parent_field_id,$main_field_name,$separator,$merge_with_db_table,$db_fields,$sql_cond,$top_parent_id,$order_by_field);
    }
    //print_r($cat_arr);
    return $cat_arr;
}
function getLevel($catid, $tbl, $main_field_id="CatID", $parent_field_id="ParentID", $main_field_name="CatName",$top_parent_id=0)
{
    global $cat_arr, $objDB,$level;
            
    $sql="SELECT * FROM `".$tbl."` WHERE `$main_field_id`='".$catid."' AND 1=1";
    //echo $sql; exit;
    $result_parent=$objDB->ddlQuery($sql);
    $row_parent=$objDB->get_row($result_parent);
        if($row_parent[$parent_field_id]!=$top_parent_id){
            $level++;
            getLevel($row_parent[$parent_field_id],$tbl,$main_field_id,$parent_field_id,$main_field_name,$top_parent_id);
        }
    return $level;
}

 



#4 jugesh

jugesh

    Advanced Member

  • Members
  • PipPipPip
  • 65 posts

Posted 24 April 2013 - 07:57 AM

please go through the code and if any further clarification needed feel free to contact



#5 lordrt

lordrt

    Advanced Member

  • Members
  • PipPipPip
  • 30 posts

Posted 24 April 2013 - 01:36 PM

thanks jugesh, will try the codes and see the results



#6 jugesh

jugesh

    Advanced Member

  • Members
  • PipPipPip
  • 65 posts

Posted 25 April 2013 - 12:24 AM

URW  lordrt... Hope to hear from u soon..

:)



#7 lordrt

lordrt

    Advanced Member

  • Members
  • PipPipPip
  • 30 posts

Posted 25 April 2013 - 01:14 AM

and thx to mac_gyver as well for the code, will give both a try today



#8 lordrt

lordrt

    Advanced Member

  • Members
  • PipPipPip
  • 30 posts

Posted 27 April 2013 - 03:44 AM

thanks both codes worked for me, but i went for mac_gyver's one which was more suited to the issue i was having



#9 jugesh

jugesh

    Advanced Member

  • Members
  • PipPipPip
  • 65 posts

Posted 27 April 2013 - 05:23 AM

great :)






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com