Repeat in nested menu

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
koolsamule
Forum Contributor
Posts: 130
Joined: Fri Sep 25, 2009 10:03 am

Repeat in nested menu

Post by koolsamule »

Hi Chaps,
I have a dynamic menu which shows the category names in a database, then the gallery names, to which they are linked:
// QUERY

Code: Select all

mysql_select_db($database_dbconnect, $dbconnect);
$query_rsCategory = "
SELECT      tbl_category.catid, 
            tbl_category.catname,
            tbl_gallery.galleryid,
            tbl_gallery.galleryname,
            tbl_gallery.FK_catid
FROM        tbl_category
INNER JOIN  tbl_gallery
            ON tbl_gallery.FK_catid=tbl_category.catid
ORDER BY    catname ASC,
            galleryname ASC";
$rsCategory = mysql_query($query_rsCategory, $dbconnect) or die(mysql_error());
//$row_rsCategory = mysql_fetch_assoc($rsCategory);
$totalRows_rsCategory = mysql_num_rows($rsCategory);
// Menu

Code: Select all

<ul>
<li><a href="#"><span>Home</span></a></li>
<li><a href="#"><span>Portfolio</span></a>
  <ul>
    <?php
      $previousCategory = '';
      if ($totalRows_rsCategory > 0) {
      // Show if recordset not empty
      do {
      if ($previousCategory != $row_rsCategory['catname']) {
      // for every Category, show the Category Name
      ?>
      <li>
      <a href="#"><?php echo $row_rsCategory['catname']; ?></a>
        <ul>
        <li>
        <a href="#"><?php echo $row_rsCategory['galleryname']; ?></a></li>
        </ul>
      </li>
      <?php $previousCategory = $row_rsCategory['catname']; } ?>
      <?php } while ($row_rsCategory = mysql_fetch_assoc($rsCategory)); ?>
      <?php } // Show if recordset not empty ?>
  </ul>
</li>
<li><a href="#"><span>Info</span></a></li>
<li><a href="#"><span>Contact</span></a>
<li><a href="#"><span>Admin</span></a></li>
</ul>
The Categories are repeating through the loop, but the problem is the Galleries aren't repeating (only one gallery per category).
How can I repeat the Galleries, for every Category?
Post Reply