473,473 Members | 1,512 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to display fetch menus from database as horizontal menus?

3 New Member
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
3 3676
Dormilich
8,658 Recognized Expert Moderator Expert
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
swatipatil05
3 New Member
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
8,658 Recognized Expert Moderator Expert
CSS definitions usually go into a CSS file that is included in the <head> section.
Feb 13 '12 #4

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

Similar topics

1
by: bjaeger | last post by:
A few months back I was "playing" with an Access 2000 application I created. I somehow disabled the delete option from all menus, toolbars, and context menus. Now I cannot do any maintenance on...
3
by: den 2005 | last post by:
Hi everyone, Here is code working on..Trying to insert record with a column with Image or VarBinary datatype in sql database from a existing jpeg image file, then retrieve this image from...
4
by: neena | last post by:
how to display values from database tables without using datagrid control in C# .net.I want to search the values from database & the search results to be shown as rows in the web form,without the use...
14
by: rolfejr | last post by:
I am trying to display a PDF in the users browser that is pulled from a binary field in our database, and keep that PDF from caching on the client computer. I can successfully pull the PDF and...
1
by: chris237 | last post by:
The following program allows a user to select a nursery rhyme from a menu. My task is to incorporate sub-menus (nursery rhyme and fairy tales) into the program. I've tried to do this but with no...
2
by: jflyingtiger | last post by:
I am running windows xp home. I usually browse with firefox, but other browsers issue the same result. I have no background color in my sub menus, side menus, and most other portions of most web...
4
by: ashwani2k | last post by:
Hello.. i develope a web projects of horoscope or astrology(http://demo.reallianzbussimart.com/allzodiac.aspx), there is an 12 Zodiaz sign and all the data call on this page through the Database ,...
1
by: ganesh22 | last post by:
Hi, my requriment is i want to display data from database in ms.word? Iam using asp.net with C# bcoz iam inserting resume in sqlserver, so i want to display that resume in word
7
by: ieda | last post by:
I want to display information when value in listbox selected. Below is my code:- This code to get value option in listbox <? $query = "select courseTitle from training where startDate > NOW()...
5
by: SafaaDalloul | last post by:
Please I want know How I can Display Image from database randomly when the web page refresh in Asp.net by C# code
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.