Jump to content

help with displaying menu hierarchy


Go to solution Solved by mac_gyver,

Recommended Posts

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?

 

menus.php
 

Link to comment
https://forums.phpfreaks.com/topic/277247-help-with-displaying-menu-hierarchy/
Share on other sites

  • Solution

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);

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;
}

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.