Copy link to clipboard
Copied
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);
<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>
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
Copy link to clipboard
Copied
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>
Copy link to clipboard
Copied
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>
Copy link to clipboard
Copied
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>
Copy link to clipboard
Copied
Thanks David, you're a star!