Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Repeat in nested menu

Guest
Dec 20, 2009 Dec 20, 2009
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:
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:
<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?


TOPICS
Server side applications
395
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Dec 20, 2009 Dec 20, 2009

This is from a test menu that I created a few weeks ago for someone else. It works correctly. Just adapt it to your own recordset:

<?php $currentCat = '';
       $first = true;
?>
<ul>
  <?php
  do {
       if ($currentCat != $row_rsMenu['category']) {
            if (!$first) {
                 echo "\n</ul>\n</li>\n";
            }
            $currentCat = $row_rsMenu['category'];
            ?>
      <li><?php echo $row_rsMenu['category']; ?>
        <ul>
      <?php
       }
       ?>
          <li><?php ec

...
Translate
LEGEND ,
Dec 20, 2009 Dec 20, 2009

The reason it's not working is because you have everything inside the conditional statement that checks for the previous category.

The following should work:

<li><a href="#"><span>Portfolio</span></a>
    <?php
      $previousCategory = '';
      $first = true;
      if ($totalRows_rsCategory > 0) {
      // Show if recordset not empty
      do {
      ?>
      <ul>
      <?php
      if ($previousCategory != $row_rsCategory['catname']) {
            if (!$first) {
                 echo "\n</ul>\n</li>\n";
            }
      // for every Category, show the Category Name
      ?>
      <li>
      <a href="#"><?php echo $row_rsCategory['catname']; ?></a>
        <ul>
      <?php
       $previousCategory = $row_rsCategory['catname'];
       }
       ?> 
        <li>
        <a href="#"><?php echo $row_rsCategory['galleryname']; ?></a></li>
      <?php
      $first = false;
       } while ($row_rsCategory = mysql_fetch_assoc($rsCategory)); ?>
      </ul>
      <?php } // Show if recordset not empty ?>
</li>

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Dec 20, 2009 Dec 20, 2009

Hi David, thank you again for your input, however, the code is not quite there, the following:

<ul>
<li><a href="#"><span>Portfolio</span></a>
    <?php
      $previousCategory = '';
      $first = true;
      if ($totalRows_rsCategory > 0) {
      // Show if recordset not empty
      do {
      ?>
      <ul>
      <?php
      if ($previousCategory != $row_rsCategory['catname']) {
            if (!$first) {
                 echo "\n</ul>\n</li>\n";
            }
      // for every Category, show the Category Name
      ?>
      <li>
      <a href="#"><?php echo $row_rsCategory['catname']; ?></a>
        <ul>
      <?php
       $previousCategory = $row_rsCategory['catname'];
       }
       ?> 
        <li>
        <a href="#"><?php echo $row_rsCategory['galleryname']; ?></a>
        </li>
        <?php
      $first = false;
       } while ($row_rsCategory = mysql_fetch_assoc($rsCategory)); ?>
      </ul>
      <?php } // Show if recordset not empty ?>
</li>
</ul>


Produces this HTML code:

<ul>
<li><a href="#"><span>Portfolio</span></a>
          <ul>
               <li><a href="#"></a></li>
          <ul>
          </ul>
</li>
      <li><a href="#">Another Gallery</a>
          <ul>
               <li><a href="#">Bristol</a></li>
          <ul>
          </ul>

     </li>
      <li><a href="#">Test Category</a>

          <ul>
               <li><a href="#">Long Islands</a></li>
          <ul>
               <li><a href="#">Old Skool</a></li>
          <ul>
               <li><a href="#">Skyline</a></li>
          </ul>
</li>
</ul>

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Dec 20, 2009 Dec 20, 2009

This is from a test menu that I created a few weeks ago for someone else. It works correctly. Just adapt it to your own recordset:

<?php $currentCat = '';
       $first = true;
?>
<ul>
  <?php
  do {
       if ($currentCat != $row_rsMenu['category']) {
            if (!$first) {
                 echo "\n</ul>\n</li>\n";
            }
            $currentCat = $row_rsMenu['category'];
            ?>
      <li><?php echo $row_rsMenu['category']; ?>
        <ul>
      <?php
       }
       ?>
          <li><?php echo $row_rsMenu['subcategory']; ?></li>
          <?php $first = false;
  } while ($row_rsMenu = mysql_fetch_assoc($rsMenu));
  ?>
   </ul>
   </li>  
</ul>

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Dec 20, 2009 Dec 20, 2009
LATEST

Thanks David, you're a star!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines