PHP Scripting Forums » PHP snippets and scripts forum

Dynamic PHP menu

(5 posts)
  • Started 3 months ago by udaysingh79
  • Latest reply from Olaf
Great offers not only for geeks!
Your Ad Here

  1. User has not uploaded an avatar

    udaysingh79
    Member

    Hi Guys,

    I have to create a dynamic menu using php and mysql. I have tried to used script given at this site at the following url: http://www.finalwebsites.com/tutorials/dynamic-navigation-list.php

    I am able to get the first level of category menu but not able to get the second and any other level of menu. i think i am not able to get the CSS for this code, can somebody tell me what exactly the thing i am missing.

    Posted 3 months ago #
  2. Hi,

    do you have an live example somewhere or paste the php code here.

    Posted 3 months ago #
  3. or better check the arrays, use this code before you call the function

    print_r($sub_menu);
    
    print_r($parent_menu);
    Posted 3 months ago #
  4. User has not uploaded an avatar

    udaysingh79
    Member

    Hi

    here is the code i am using:

    $link = mysql_connect('localhost','root','');
    mysql_select_db('globalbiz',$link);

    echo $sql = "SELECT id, cat_name, page_url, parent_id FROM test_cat ORDER BY parent_id, id ASC";
    $items = mysql_query($sql);
    while ($obj = mysql_fetch_object($items)) {
    if ($obj->parent_id == 0) {
    $parent_menu[$obj->id]['label'] = $obj->cat_name;
    $parent_menu[$obj->id]['link'] = $obj->page_url;
    } else {
    $sub_menu[$obj->id]['parent'] = $obj->parent_id;
    $sub_menu[$obj->id]['label'] = $obj->cat_name;
    $sub_menu[$obj->id]['link'] = $obj->page_url;
    $parent_menu[$obj->parent_id]['count']++;
    }
    }
    mysql_free_result($items);

    /////end first block
    //echo"

    "; print_r($sub_menu);exit;
    ///second block
    
    function dyn_menu($parent_array, $sub_array, $qs_val = "menu", $main_id = "nav", $sub_id = "subnav", $extra_style = "foldout") {
        $menu = "<ul id=\"".$main_id."\">\n";
        foreach ($parent_array as $pkey => $pval) {
            if (!empty($pval['count'])) {
                $menu .= "
    
  5. ".$pval['label']."
  6. \n"; } else { $menu .= "
  7. ".$pval['label']."
  8. \n"; } if (!empty($_REQUEST[$qs_val])) { $menu .= "<ul id=\"".$sub_id."\">\n"; foreach ($sub_array as $sval) { if ($pkey == $_REQUEST[$qs_val] && $pkey == $sval['parent']) { $menu .= "
  9. ".$sval['label']."
  10. \n"; } } $menu .= "\n"; } } $menu .= "\n"; return $menu; } ////end second block if (!empty($_REQUEST[$qs_val])) { $menu .= "<ul id=\"".$sub_id."\">\n"; foreach ($sub_array as $sval) { if ($pkey == $_REQUEST[$qs_val] && $pkey == $sval['parent']) { $menu .= "
  11. ".$sval['label']."
  12. \n"; } } $menu .= "\n"; } function rebuild_link($link, $parent_var, $parent_val) { $link_parts = explode("?", $link); $base_var = "?".$parent_var."=".$parent_val; if (!empty($link_parts[1])) { $link_parts[1] = str_replace("&", "##", $link_parts[1]); $parts = explode("##", $link_parts[1]); $newParts = array(); foreach ($parts as $val) { $val_parts = explode("=", $val); if ($val_parts[0] != $parent_var) { array_push($newParts, $val); } } if (count($newParts) != 0) { $qs = "&".implode("&", $newParts); } return $link_parts[0].$base_var.$qs; } else { return $link_parts[0].$base_var; } } //// third block echo dyn_menu($parent_menu, $sub_menu, "menu", "nav", "subnav");
    Posted 3 months ago #
  13. Hi,

    do you tried the original code first?

    Posted 3 months ago #

RSS feed for this topic

Reply

You must log in to post.