473,545 Members | 721 Online
Bytes | Software Development & Data Engineering Community
+ 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 3685
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
3343
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 the database because I cannot delete anything. I do not have a database password set up, or users with security and rights. I cannot find an...
3
5226
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 database and display it in a Image web control dynamically(at runtime). The process after being displayed in the web control, user click insert/add...
4
6925
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 of datagrid.So any one plz help me.
14
11864
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 display it using the following code: Response.ContentType = "application/pdf" Response.BinaryWrite objRS("Attachment") where objRS("Attachment")...
1
15705
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 success as you can see below this program. Any help with this would be greatly appreciated. If anyone has a non-related menu program i could have a look...
2
3934
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 sites. In fact most web sites come up black and white with ony link highlights in blue. I believe this may have occurred after selecting and entering...
4
1612
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 , in this page i there is an one sql query ---- ...
1
1688
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
6938
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() "; $result = mysql_query($query); print "<SELECT name=item>"; while ($line = mysql_fetch_array($result))
5
2532
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
7465
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7398
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7416
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7752
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5969
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5325
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3441
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1878
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 we have to send another system
1
1013
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.