By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,986 Members | 2,030 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,986 IT Pros & Developers. It's quick & easy.

How to display fetch menus from database as horizontal menus?

P: 3
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 ..




Expand|Select|Wrap|Line Numbers
  1. <?php
  2. // include your database connection file here (if available)
  3. // for example require_once("../../db.php");
  4. include_once('dbconn.php');
  5.  
  6. $sql = "SELECT id, label, link_url, parent_id FROM dyn_menu ORDER BY parent_id, id ASC";
  7. $items = mysql_query($sql);
  8. while ($obj = mysql_fetch_object($items)) 
  9. {
  10.     if ($obj->parent_id == 0) {
  11.  
  12.         $parent_menu[$obj->id]['label'] = $obj->label;
  13.  
  14.         $parent_menu[$obj->id]['link'] = $obj->link_url;
  15.         echo "| ";
  16.     } else {
  17.         $sub_menu[$obj->id]['parent'] = $obj->parent_id;
  18.         $sub_menu[$obj->id]['label'] = $obj->label;
  19.         $sub_menu[$obj->id]['link'] = $obj->link_url;
  20.         if (!isset($parent_menu[$obj->parent_id]['count'])) {
  21.  
  22.             $parent_menu[$obj->parent_id]['count'] = 0;
  23.  
  24.         }
  25.         $parent_menu[$obj->parent_id]['count']++;
  26.     }
  27. }
  28. mysql_free_result($items);
  29.  
  30. function dyn_menu($parent_array, $sub_array, $qs_val = "menu", $main_id = "nav", $sub_id = "subnav", $extra_style = "foldout") {
  31.     $menu = "<ul id=\"".$main_id."\"> ";
  32.     foreach ($parent_array as $pkey => $pval) {
  33.         if (!empty($pval['count'])) {
  34.             $menu .= "  <li><a class=\"".$extra_style."\" href=\"".$pval['link']."?".$qs_val."=".$pkey."\">".$pval['label']."</a></li> ";
  35.         } else {
  36.             $menu .= "  <li><a href=\"".$pval['link']."\">".$pval['label']."</a></li> ";
  37.         }
  38.         if (!empty($_REQUEST[$qs_val])) {
  39.             $menu .= "<ul id=\"".$sub_id."\"> ";
  40.              foreach ($sub_array as $sval) {
  41.                 if ($pkey == $_REQUEST[$qs_val] && $pkey == $sval['parent']) {
  42.                     $menu .= "<li><a href=\"".rebuild_link($sval['link'], $qs_val, $sval['parent'])."\" target=\"_blank\">".$sval['label']."</a></li> ";
  43.                 }
  44.             }
  45.             $menu .= "</ul> ";
  46.         }
  47.     }
  48.     $menu .= "</ul>\n";
  49.     return $menu;
  50.   }
  51. function dyn_menu_folded($parent_array, $sub_array, $qs_val = "menu", $main_id = "nav", $sub_id = "subnav", $extra_style = "foldout") {
  52.     $menu = "<ul id=\"".$main_id."\">\n";
  53.     foreach ($parent_array as $pkey => $pval) {
  54.         if (!empty($pval['count'])) {
  55.             $menu .= "  <li><a class=\"".$extra_style."\" href=\"".$pval['link']."?".$qs_val."=".$pkey."\">".$pval['label']."</a></li>\n";
  56.         } else {
  57.             $menu .= "  <li><a href=\"".$pval['link']."\">".$pval['label']."</a></li>\n";
  58.         }
  59.         //if (!empty($_REQUEST[$qs_val])) {
  60.             $menu .= "<ul id=\"".$sub_id."\">\n";
  61.             foreach ($sub_array as $sval) {
  62.                 if ($pkey == $sval['parent']) { //
  63.                     $menu .= "<li><a href=\"".$sval['link']."\" target=\"_blank\">".$sval['label']."</a></li>\n";
  64.                 }
  65.             }
  66.             $menu .= "</ul>\n";
  67.         //}
  68.     }
  69.     $menu .= "</ul>\n";
  70.     return $menu;
  71. }
  72. function rebuild_link($link, $parent_var, $parent_val) {
  73.     $link_parts = explode("?", $link);
  74.     $base_var = "?".$parent_var."=".$parent_val;
  75.     if (!empty($link_parts[1])) {
  76.         $link_parts[1] = str_replace("&amp;", "##", $link_parts[1]);
  77.         $parts = explode("##", $link_parts[1]);
  78.         $newParts = array();
  79.         foreach ($parts as $val) {
  80.             $val_parts = explode("=", $val);
  81.             if ($val_parts[0] != $parent_var) {
  82.                 array_push($newParts, $val);
  83.             }
  84.         }
  85.         if (count($newParts) != 0) {
  86.             $qs = "&amp;".implode("&amp;", $newParts);
  87.         }
  88.         return $link_parts[0].$base_var.$qs;
  89.     } else {
  90.         return $link_parts[0].$base_var;
  91.     }
  92. }
  93. ?>
  94. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  95. <html>
  96. <head>
  97. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  98. <title>Dynamic PHP navigation list example page</title>
  99.  
  100. <script type="text/javascript"  src="dhtml-menu.js"  language="javascript"></script>
  101.  
  102.  
  103. <style type="text/css">
  104. <!--
  105. body {
  106.     font-family:Arial, Helvetica, sans-serif;
  107.     padding:0 15px;
  108. }
  109. p, li { font-size:12px;line-height:16px; }
  110.  
  111. #disply
  112. {
  113. width:40 px;
  114. height:40px;
  115. display
  116. -->
  117. </style>
  118. </head>
  119.  
  120. <body>
  121. <!--<h2>Dynamic PHP navigation list tutorial (example)</h2>
  122.  
  123. <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>
  124. <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>-->
  125. <div style="float:right;width:340px;">
  126.  
  127. <?php
  128. //echo dyn_menu_folded($parent_menu, $sub_menu, "menu", "nav", "subnav");
  129. ?>
  130.  
  131. </div>
  132. <?php
  133. echo dyn_menu($parent_menu, $sub_menu, "menu", "nav", "subnav");
  134. ?>
  135.  
  136. <p style="clear:both;"></p>
  137.  
  138. </body>
  139. </html>

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


Any help would be appriciated...

Thax..
Feb 11 '12 #1
Share this Question
Share on Google+
3 Replies


Dormilich
Expert Mod 5K+
P: 8,639
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.
Feb 11 '12 #2

P: 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...
Feb 13 '12 #3

Dormilich
Expert Mod 5K+
P: 8,639
CSS definitions usually go into a CSS file that is included in the <head> section.
Feb 13 '12 #4

Post your reply

Sign in to post your reply or Sign up for a free account.