473,408 Members | 2,087 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

php/mysql dynamic multi-level menu problem

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("SELECT DISTINCT `Manufacturer` FROM `products`");

/* Get list of products and details */
$result = mysql_query("SELECT `ProdID`,`Manufacturer`, `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.php">Company</a>
<ul>
<li>
<a href="index.php">Home Page</a>
</li>
<li>
<a href="aboutus.php">About us</a>
</li>
<li>
<a href="contactus.php">Contact us</a>
</li>
</ul>
</li>

<li>
<a href="#">Products</a>
<ul>
<li>
<a href="" class="daddy">By manufacturer</a>
<ul>
<? while ($resultset = mysql_fetch_assoc($result_manu))

{
$Manufacturer=$resultset['Manufacturer'];
echo "<li>";
echo "<a href='' class='daddy'>$Manufacturer</a>";
echo "<ul>";
while ($resultset2 = mysql_fetch_assoc($result))
{
$ProdID=$resultset2['ProdID'];
$NameModel=$resultset2['NameModel'];
echo "<li>";
echo "<a href='products.php?ProdID=$ProdID'>$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.php">Company</a>
<ul>
<li>
<a href="index.php">Home Page</a>
</li>
<li>
<a href="aboutus.php">About us</a>
</li>
<li>
<a href="contactus.php">Contact us</a>
</li>
</ul>
</li>

<li>
<a href="#">Products</a>
<ul>
<li>
<a href="" class="daddy">By manufacturer</a>
<ul>
<li><a href='' class='daddy'>Kenwood</a>
<ul>
<li>
<a href='products.php?ProdID=1'>TK-270G/370G</a>
</li>
<li>
<a href='products.php?ProdID=4'>tester</a>
</li>
<li>
<a href='products.php?ProdID=5'>Chef</a>
</li>
</ul>
</li>
<li><a href='' class='daddy'>test 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.php">Company</a>
<ul>
<li>
<a href="index.php">Home Page</a>
</li>
<li>
<a href="aboutus.php">About us</a>
</li>
<li>
<a href="contactus.php">Contact us</a>
</li>
</ul>
</li>

<li>
<a href="#">Products</a>
<ul>
<li>
<a href="" class="daddy">By manufacturer</a>
<ul>
<li><a href='' class='daddy'>Kenwood</a>
<ul>
<li>
<a href='products.php?ProdID=1'>TK-270G/370G</a>
</li>
<li>
<a href='products.php?ProdID=5'>Chef</a>
</li>
</ul>
</li>
<li><a href='' class='daddy'>test manufacturer</a>
<ul>
<li>
<a href='products.php?ProdID=4'>tester</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 19071
Niheel
2,460 Expert Mod 2GB
Basically you can't get the menu's to display the products under the right manufacturer?
Jan 25 '06 #2
Niheel
2,460 Expert Mod 2GB
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
Wow! Thanks! It works great! I'll go and study why it works now :D

Thanks again

pete
Jan 25 '06 #4
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
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...
0
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...
0
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 -...
0
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...
0
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...
1
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...
3
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...
1
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...
3
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...
1
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 { ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
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
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...

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.