SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict
    Join Date
    Aug 2009
    Posts
    299
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Repeat in nested menu

    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?

  2. #2
    SitePoint Wizard
    Join Date
    Mar 2002
    Location
    Bristol, UK
    Posts
    2,240
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You're almost there, try using this instead:

    Code php:
        <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>
                <?php
            }
            ?>
            <li><a href="#"><?php echo $row_rsCategory['galleryname']; ?></a></li>
            <?php
            if($previousCategory != $row_rsCategory['catname']) {
                ?>
                </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>

  3. #3
    SitePoint Addict
    Join Date
    Aug 2009
    Posts
    299
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi SJH, thanks for the code, but it doesn't work, getting HTTP500 Error. . . .any ideas?

  4. #4
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Do you have a .htaccess file affecting these requests?

  5. #5
    SitePoint Addict
    Join Date
    Aug 2009
    Posts
    299
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nope, don't have a htaccess file . . .however, I've got the menu displaying the correct way:
    PHP Code:
    <?php $currentCat '';       
        
    $first true
        
    ;?>
        <ul>  
            <?php   
            
    do {       
            if (
    $currentCat != $row_rsCategory['catname']) {            
            if (!
    $first) {                 
            echo 
    "\n</ul>\n</li>\n";            
            }            
            
    $currentCat $row_rsCategory['catname'];            
            
    ?>      
                <li><?php echo $row_rsCategory['catname']; ?>       
                    <ul>      
                    <?php }?>          
                        <li><?php echo $row_rsCategory['galleryname']; ?></li>          
                    <?php $first false;  
                    } while (
    $row_rsCategory mysql_fetch_assoc($rsCategory));  
                    
    ?>   
                    </ul>   
                </li>   
        </ul>
    All I need now is some show/hide code, controlled by the category name. I've looked on Google, found a few javascript tutorials, but can't find one that incorporates a PHP/MySQL loop, in a UL/LI menu, most of the ones I've found have static menu options. . . .????


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •