473,597 Members | 2,190 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

php/mysql dynamic multi-level menu problem

8 New Member
I am trying to implement a CSS hierarchical unfolding menu on a site. The thing is, it needs to be dynamically populated from the results of a database query. I previously had the menu working but then it was ‘hard coded’ and not built on the fly.

Menu description:

2 top level items “Company” and “Products” (we will ignore “Company” since it is still hard coded and not causing a problem.

Below “Products” we have hard coded “By Manufacturer”. So you hover over “Products” and it unfolds and “By Manufacturer” is visible. If you hover over that, a list of manufacturers should open to the right. This list is extracted from the data base using SELECT DISTINCT.

Then if you hover over a manufacturer, another level unfolds which should contain the products of that manufacturer.

My problem is that the list of products includes all products in the database, not just those from the relevant manufacturer. The menus of the other manufacturers are empty.

The manufacturer under which all the products appear, is the manufacturer of the first product in the database.

[PHP]
<? require("inc/connect.txt");
/* Connecting to a database and retrieve data */
$mysql_access = mysql_connect(" localhost", "$un", "$pw") or die("Error connecting to database server: ".mysql_error() );
mysql_select_db ($db, $mysql_access) or die("Error connecting to database: ".mysql_error() );//always gotta do some error checking...

/* Get list of unique manufacturers */
$result_manu = mysql_query("SE LECT DISTINCT `Manufacturer` FROM `products`");

/* Get list of products and details */
$result = mysql_query("SE LECT `ProdID`,`Manuf acturer`, `NameModel` FROM `products`");

//always gotta do some error checking...
if (!$result)
{exit("Error in SQL");} ?>

<!-- Begin unfolding menu code/structure when viewed without the accompanying stylesheet, it should display as a multi-level list of links which makes it easy to see which category items are in. -->

<ul id="nav">

<li>
<a href="aboutus.p hp">Company</a>
<ul>
<li>
<a href="index.php ">Home Page</a>
</li>
<li>
<a href="aboutus.p hp">About us</a>
</li>
<li>
<a href="contactus .php">Contact us</a>
</li>
</ul>
</li>

<li>
<a href="#">Produc ts</a>
<ul>
<li>
<a href="" class="daddy">B y manufacturer</a>
<ul>
<? while ($resultset = mysql_fetch_ass oc($result_manu ))

{
$Manufacturer=$ resultset['Manufacturer'];
echo "<li>";
echo "<a href='' class='daddy'>$ Manufacturer</a>";
echo "<ul>";
while ($resultset2 = mysql_fetch_ass oc($result))
{
$ProdID=$result set2['ProdID'];
$NameModel=$res ultset2['NameModel'];
echo "<li>";
echo "<a href='products. php?ProdID=$Pro dID'>$NameModel </a>";
echo "</li>";
}
echo "</ul>";
echo "</li>";
}?>
</ul>
</li>
</ul>
</li>
</ul>
[/PHP]

Below is the code which is rendered by the browser when displayed without the appropriate CSS stylesheet and JavaScript that makes it work.
[HTML]

<!-- Begin unfolding menu code/structure when viewed without the accompanying stylesheet, it should display as a multi-level list of links which makes it easy to see which category items are in. -->

<ul id="nav">

<li>
<a href="aboutus.p hp">Company</a>
<ul>
<li>
<a href="index.php ">Home Page</a>
</li>
<li>
<a href="aboutus.p hp">About us</a>
</li>
<li>
<a href="contactus .php">Contact us</a>
</li>
</ul>
</li>

<li>
<a href="#">Produc ts</a>
<ul>
<li>
<a href="" class="daddy">B y manufacturer</a>
<ul>
<li><a href='' class='daddy'>K enwood</a>
<ul>
<li>
<a href='products. php?ProdID=1'>T K-270G/370G</a>
</li>
<li>
<a href='products. php?ProdID=4'>t ester</a>
</li>
<li>
<a href='products. php?ProdID=5'>C hef</a>
</li>
</ul>
</li>
<li><a href='' class='daddy'>t est manufacturer</a>
<ul>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
[/HTML]

...and this is the code that I am aiming to have rendered..

[HTML]

<!-- Begin unfolding menu code/structure when viewed without the accompanying stylesheet, it should display as a multi-level list of links which makes it easy to see which category items are in. -->

<ul id="nav">

<li>
<a href="aboutus.p hp">Company</a>
<ul>
<li>
<a href="index.php ">Home Page</a>
</li>
<li>
<a href="aboutus.p hp">About us</a>
</li>
<li>
<a href="contactus .php">Contact us</a>
</li>
</ul>
</li>

<li>
<a href="#">Produc ts</a>
<ul>
<li>
<a href="" class="daddy">B y manufacturer</a>
<ul>
<li><a href='' class='daddy'>K enwood</a>
<ul>
<li>
<a href='products. php?ProdID=1'>T K-270G/370G</a>
</li>
<li>
<a href='products. php?ProdID=5'>C hef</a>
</li>
</ul>
</li>
<li><a href='' class='daddy'>t est manufacturer</a>
<ul>
<li>
<a href='products. php?ProdID=4'>t ester</a>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
[/HTML]

I've a feeling that I'm close. Can anyone see my mistake please?

kind regards

pete
Jan 25 '06 #1
4 19097
Niheel
2,456 Recognized Expert Moderator Top Contributor
Basically you can't get the menu's to display the products under the right manufacturer?
Jan 25 '06 #2
Niheel
2,456 Recognized Expert Moderator Top Contributor
The problem was in your while statement. I moved the 2nd Query into the while statement and added the WHERE clause.

Try this code:
Expand|Select|Wrap|Line Numbers
  1.  
  2. <? require("inc/connect.txt"); 
  3. /* Connecting to a database and retrieve data */ 
  4. $mysql_access = mysql_connect("localhost", "$un", "$pw") or die("Error connecting to database server: ".mysql_error()); 
  5. mysql_select_db($db, $mysql_access) or die("Error connecting to database: ".mysql_error());//always gotta do some error checking... 
  6. /* Get list of unique manufacturers */ 
  7. $result_manu = mysql_query("SELECT DISTINCT `Manufacturer` FROM `products`"); 
  8. # MOVE THIS INTO TO YOUR WHILE STATEMENT
  9. #/* Get list of products and details */ 
  10. #$result = mysql_query("SELECT `ProdID`,`Manufacturer`, `NameModel` FROM `products`"); 
  11. //always gotta do some error checking... 
  12. if (!$result) 
  13.   {exit("Error in SQL");} ?> 
  14. <!-- Begin unfolding menu code/structure when viewed without the accompanying stylesheet, it should display as a multi-level list of links which makes it easy to see which category items are in. --> 
  15. <ul id="nav"> 
  16.    <li> 
  17.     <a href="aboutus.php">Company</a> 
  18.       <ul> 
  19.          <li> 
  20.           <a href="index.php">Home Page</a> 
  21.          </li> 
  22.          <li> 
  23.           <a href="aboutus.php">About us</a> 
  24.          </li> 
  25.          <li> 
  26.           <a href="contactus.php">Contact us</a> 
  27.          </li> 
  28.       </ul> 
  29.    </li> 
  30.    <li> 
  31.     <a href="#">Products</a> 
  32.       <ul> 
  33.          <li> 
  34.           <a href="" class="daddy">By manufacturer</a> 
  35.              <ul> 
  36.               <? while ($resultset = mysql_fetch_assoc($result_manu)) 
  37.               $Manufacturer=$resultset['Manufacturer']; 
  38.                  echo "<li>"; 
  39.                   echo "<a href='' class='daddy'>$Manufacturer</a>"; 
  40.                      echo "<ul>";
  41.       # 2nd QUERY MOVED HERE
  42.       /* Get list of products and details */ 
  43.       $result = mysql_query("SELECT `ProdID`,`Manufacturer`, `NameModel` FROM `products` WHERE Manufacturer='$Manufacturer'"); 
  44.                       while ($resultset2 = mysql_fetch_assoc($result)) 
  45.                       $ProdID=$resultset2['ProdID']; 
  46.                       $NameModel=$resultset2['NameModel']; 
  47.                         echo "<li>"; 
  48.                          echo "<a href='products.php?ProdID=$ProdID'>$NameModel</a>"; 
  49.                         echo "</li>"; 
  50.                      echo "</ul>"; 
  51.                   echo "</li>"; 
  52. }?> 
  53.               </ul> 
  54.            </li> 
  55.         </ul> 
  56.     </li> 
  57. </ul> 
  58.  
If you run into errors; paste them here.
Jan 25 '06 #3
snowweb
8 New Member
Wow! Thanks! It works great! I'll go and study why it works now :D

Thanks again

pete
Jan 25 '06 #4
jamina1
1 New Member
That does work great, but how would one add a THIRD level?

I've got category as the main selector, and it lists each title under each category - but I've got subcategories defined as well..

Is this possble? Here's my code

Expand|Select|Wrap|Line Numbers
  1. $result_cat = mysql_query("SELECT DISTINCT `category` FROM navigation");
  2.  
  3. /*Begin Menu Navigation*/
  4. <div id="nav">
  5. <ul>
  6.     <? while ($resultset = mysql_fetch_assoc($result_cat)) {
  7.         $category=$resultset['category'];
  8.             echo "<li>";
  9.             echo "<a href='#' class='daddy'>$category</a>";
  10.                 echo "<ul>";
  11.  
  12. $result = mysql_query("SELECT `tutid`,`category`,`title`,`subcat` FROM navigation WHERE category='$category'");
  13.  
  14. ## Need a switch or If statement here, some of these secondary items will have tertiary items under them, how do I figure out if they do and then list them if they do? ##
  15.  
  16.     while ($resultset2 = mysql_fetch_assoc($result)) {
  17.     $tutid=$resultset2['tutid'];
  18.     $title=$resultset2['title'];
  19.         echo "<li>";
  20.         echo "<a href='tutorials.php?tutid=$tutid'>$Title</a>";                    echo "</li>"; }
  21.     echo "</ul>";
  22.     echo "</li>";
  23. }
  24.  
Mar 27 '06 #5

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

Similar topics

0
3302
by: Randell D. | last post by:
Folks, I have installed MySQL v4 (client, server and development rpm's). I've tried and failed to use the recommended mysqladmin to set a root password after the installation (I have another post open on the MySQL newsgroup about this). I'm attempting an alternative solution and using webmin (www.webmin.com) to create/manage access to my databases. Webmin reports "Warning: The Perl module DBD::mysql is not installed on your system, so...
0
4917
by: Ganbold | last post by:
Hi, I'm new to multi-threaded programming and reading the book "Programming with POSIX Threads" and trying to understand concepts and coding. What I'm trying to do is to rewrite mysql client application (which calculates ISP dial-up customers' billing) into multi-threaded version.
0
447
by: darin Ginther | last post by:
RE:mysql-3.23.58-1.73.i386.rpm mysqld does not exist on my system... I think this is just the client software.. I have to install the database itself from source, IE - mysql-3.23.58-pc-linux-i686.tar.gz?? # rpm -qa | grep mysql mysql-3.23.58-1.73
0
1471
by: Yun Guan | last post by:
Hello mysql gurus, I am trying to run perl on mysql database on Red Hat box. I want to install DBI and DBD:mysql using CPAN: perl -MCPAN -e shell cpan>install DBI The above succeeded, but cpan>install DBD::mysql
0
1756
by: Vic | last post by:
Hi all, When I test the Delete multi table function in MySQL, DELETE table_name ...] FROM table-references I accidentally delete all data in one table. All data in that table are gone when I try to select them out in Control Center. But when I go into the /mysql/data/mydatabase/, I see a MYD, MYI, frm for that table. And it seems that data is still inside the MYD, althought it's
1
2609
by: Yun Guan | last post by:
Hello, I have problems installing DBD:mysql on Red Hat Linux 9, either from CPAN, or mannual install. The error is the same, something related to this Kid.pm file under perl. Should I reinstall Perl? Perl came with Red Hat Linux 9 installation. Please help. 1.) from CPAN: perl -MCPAN -e shell
3
3964
by: Leo J. Hart IV | last post by:
OK, here's another question for the experts: I am building a multi-step (3 steps actually) form using a panel for each step and hiding/displaying the appropriate panel/panels depending on which step you're on. This all works fine, but I ran into some trouble when I started creating controls dynamically in my code-behind file. Each panel contains a table which is filled with various radio buttons, text fields and the such which are...
1
2841
by: edfialk | last post by:
Hi all, I'm desperately trying to get a simple mysql connection working in php 4.3.9 and I think I have a doozy for you guys. First of all, I didn't set up ANY of this system, I'm just working with it. So, on Redhat, we have two versions of PHP - Apache uses 4.3.9, there's also a 4.4.2 that is default from command-line. 4.3.9 was installed by RPM, 4.4.2 was built from source. I can get mysql.so loaded and working fine in 4.4.2, but...
3
8811
by: menzies | last post by:
Hi, I"m new to this forum, but I have been trying all day to install DBD::mysql onto my Intel MacBook. I've read lots of forums pages and none have gotten me to a successful 'make test' or a successful 'sudo make install.' Before every attempt I even do a sudo make distclean to make sure I haven't gotten things mucked up from a prior attempt. OS: Mac OS X 10.4.10 MySQL: v5.0.41 for Mac OSX-i686 DBI: v1.58 (installed fine using CPAN)...
1
8866
by: chaosbuddha | last post by:
Hi! I am trying to set-up mysql-zrm to take backups of a remote mysql server. The mysqlhotcopy command is throwing up the following error: Output of command: 'mysqlhotcopy' is { install_driver(mysql) failed: Can't locate DBD/mysql.pm in @INC (@INC contains: /usr/lib/perl5/5.8.8/i586-linux-thread-multi /usr/lib/perl5/5.8.8 /usr/lib/perl5 /site_perl/5.8.8/i586-linux-thread-multi /usr/lib/perl5/site_perl/5.8.8 /usr/lib...
0
7969
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7886
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8272
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8381
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8035
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 most users, this new feature is actually very convenient. If you want to control the update process,...
1
5847
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
3886
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
1494
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1238
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.