How to display fetch menus from database as horizontal menus?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • swatipatil05
    New Member
    • Jan 2012
    • 3

    #1

    How to display fetch menus from database as horizontal menus?

    hii,

    I am going to build menus and sub menus.I have fetch data from the table and it sucessfully display the menus and submenus..
    But I have problem is that the menus displayed as un -orderd list .That menu i want to display horizontaly..

    Here is the php code for that ..




    Code:
    <?php
    // include your database connection file here (if available)
    // for example require_once("../../db.php");
    include_once('dbconn.php');
    
    $sql = "SELECT id, label, link_url, parent_id FROM dyn_menu 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->label;
    		
            $parent_menu[$obj->id]['link'] = $obj->link_url;
    		echo "| ";
        } else {
            $sub_menu[$obj->id]['parent'] = $obj->parent_id;
            $sub_menu[$obj->id]['label'] = $obj->label;
            $sub_menu[$obj->id]['link'] = $obj->link_url;
            if (!isset($parent_menu[$obj->parent_id]['count'])) {
    		
    			$parent_menu[$obj->parent_id]['count'] = 0;
    			
    		}
    		$parent_menu[$obj->parent_id]['count']++;
        }
    }
    mysql_free_result($items);
    
    function dyn_menu($parent_array, $sub_array, $qs_val = "menu", $main_id = "nav", $sub_id = "subnav", $extra_style = "foldout") {
        $menu = "<ul id=\"".$main_id."\"> ";
        foreach ($parent_array as $pkey => $pval) {
            if (!empty($pval['count'])) {
                $menu .= "  <li><a class=\"".$extra_style."\" href=\"".$pval['link']."?".$qs_val."=".$pkey."\">".$pval['label']."</a></li> ";
            } else {
                $menu .= "  <li><a href=\"".$pval['link']."\">".$pval['label']."</a></li> ";
            }
            if (!empty($_REQUEST[$qs_val])) {
                $menu .= "<ul id=\"".$sub_id."\"> ";
                 foreach ($sub_array as $sval) {
                    if ($pkey == $_REQUEST[$qs_val] && $pkey == $sval['parent']) {
                        $menu .= "<li><a href=\"".rebuild_link($sval['link'], $qs_val, $sval['parent'])."\" target=\"_blank\">".$sval['label']."</a></li> ";
                    }
                }
                $menu .= "</ul> ";
            }
        }
        $menu .= "</ul>\n";
        return $menu;
      }
    function dyn_menu_folded($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 .= "  <li><a class=\"".$extra_style."\" href=\"".$pval['link']."?".$qs_val."=".$pkey."\">".$pval['label']."</a></li>\n";
            } else {
                $menu .= "  <li><a href=\"".$pval['link']."\">".$pval['label']."</a></li>\n";
            }
            //if (!empty($_REQUEST[$qs_val])) {
                $menu .= "<ul id=\"".$sub_id."\">\n";
                foreach ($sub_array as $sval) {
                    if ($pkey == $sval['parent']) { //
                        $menu .= "<li><a href=\"".$sval['link']."\" target=\"_blank\">".$sval['label']."</a></li>\n";
                    }
                }
                $menu .= "</ul>\n";
            //}
        }
        $menu .= "</ul>\n";
        return $menu;
    }
    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("&amp;", "##", $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 = "&amp;".implode("&amp;", $newParts);
            }
            return $link_parts[0].$base_var.$qs;
        } else {
            return $link_parts[0].$base_var;
        }
    }
    ?>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <title>Dynamic PHP navigation list example page</title>
    
    <script type="text/javascript"  src="dhtml-menu.js"  language="javascript"></script>
    
    
    <style type="text/css">
    <!--
    body {
    	font-family:Arial, Helvetica, sans-serif;
    	padding:0 15px;
    }
    p, li { font-size:12px;line-height:16px; }
    
    #disply
    {
    width:40 px;
    height:40px;
    display
    -->
    </style>
    </head>
    
    <body>
    <!--<h2>Dynamic PHP navigation list tutorial (example)</h2>
    
    <p>The unordered list below is generated by the code used in the tutorial. Click the main items (Job groups) to make the sub-items visible.</p>
    <p>We created also a second menu which shows all sub items, to get this effect we removed the code <strong>$pkey == $_REQUEST[$qs_val] && </strong> from the second <strong>foreach</strong> loop and the if statement <strong>if (!empty($_REQUEST[$qs_val]))</strong>.</p>-->
    <div style="float:right;width:340px;">
    
    <?php
    //echo dyn_menu_folded($parent_menu, $sub_menu, "menu", "nav", "subnav");
    ?>
    
    </div>
    <?php
    echo dyn_menu($parent_menu, $sub_menu, "menu", "nav", "subnav");
    ?>
    
    <p style="clear:both;"></p>
    
    </body>
    </html>

    .........Please Help me out..


    Any help would be appriciated...

    Thax..
    Last edited by Dormilich; Feb 11 '12, 11:35 AM. Reason: please use code tags when posting code
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    whether you display this menu horizontal or vertical is a CSS problem (because that is where you define the styling). PHP is providing only the bare content.

    Comment

    • swatipatil05
      New Member
      • Jan 2012
      • 3

      #3
      Im new in php mysql..I dont understand how and where should i palce the css..I tried bt nothing come out

      The out put Like some as way..

      Home
      About Us
      Contact Us
      Help.


      When i clik on Home ....HOme1 & Home2 appears and when click on second it hides first menus&submenus. . THis is fine, But Im confused how should i pace css here...


      Please give me solution for this..

      thanx...

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        CSS definitions usually go into a CSS file that is included in the <head> section.

        Comment

        Working...