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

dynamic nav with categories and subcategories

New Here ,
Apr 07, 2009 Apr 07, 2009

Copy link to clipboard

Copied

Hey guys im in the middle of customizing a php cms app to fit my site...im basically reusing some of its code.
im currently working on the nav which is an accordion spry i will be using php to get categories and subcategories and echoing them so as to create the accordion.


the subcategories should appear under its category. In the database there are one category for many subcats and one subcat for mutiple products...so tbl_subcat has its own id plus the cat_id as the foreign key.
the origianl nav script echoed <li></li> for only categories..i adopted it for divs
here is the page http://vaughntucker.com/exflowers/about.php?


The problem im having is loading all the subcats under each category it only does one row and it repeats the same subcat for the rest of the categories so i know my php
logic is off but im stumped...

here is the entire code http://vaughntucker.com/exflowers/about.txt

Thanks

TOPICS
Server side applications

Views

696

Translate

Translate

Report

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
New Here ,
Apr 08, 2009 Apr 08, 2009

Copy link to clipboard

Copied

Ok guys i know my code maybe too long to go through so i'll simplyfy im only using one do while statement it partially works, i tried 2 but i had the logic messed up i think. Come on guys help me out here its probably something simple that you'll figure out.

here is the code that gets the category and sub category

//get all cats for side nav// //
mysql_select_db($database, $makeconnection);
$sql_get_cats = "SELECT * FROM tbl_cat ORDER BY cat_id ASC";
$get_cats = mysql_query($sql_get_cats, $makeconnection) or die(mysql_error());
$row_get_cats = mysql_fetch_assoc($get_cats);
$totalRows_get_cats = mysql_num_rows($get_cats);


//get all subcats for side nav//ORDER BY catsub_id ASC  ASC*/
mysql_select_db($database, $makeconnection);
$sql_get_subcats = "SELECT * FROM tbl_subcat ";
$get_subcats = mysql_query($sql_get_subcats, $makeconnection) or die(mysql_error());
$row_get_subcats = mysql_fetch_assoc($get_subcats);
$totalRows_get_subcats = mysql_num_rows($get_subcats);





here is the do while code that prints them



<?php
      do {
if($row_get_subcats['subcat_id']==$current_subcat){
$link_look='class="selectedPage"';

}else{
$link_look='';

}
?>
<div class="AccordionPanel">
<div class="AccordionPanelTab"><a href="about.php?cat=<?php echo $row_get_cats['cat_id']; ?>"
>
<?php echo $row_get_cats['cat_name']; ?></a></div>

<div class="AccordionPanelContent"><a href="about.php?subcat=<?php echo $row_get_subcats['subcat_id']; ?>&cat=<?php echo $row_get_cats['cat_id']; ?>"
<?php echo $link_look;?>>
<?php
//if($row_get_cats['cat_id'] == $row_get_subcats['catsub_id']){}

echo $row_get_subcats['subcat_name'];?></a></div></div>

  <?php  }while ($row_get_cats = mysql_fetch_assoc($get_cats));  ?>

Votes

Translate

Translate

Report

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 ,
Apr 09, 2009 Apr 09, 2009

Copy link to clipboard

Copied

LATEST

If I understand correctly, you're trying to loop through a series of categories and assign the relevant sub-categories to them. Whether you're building an accordion or any other structure is irrelevant. What matters is the logic of how you loop through things.

There are two ways to do this. The first involves nested loops, and can be done with two recordsets. The other way, which is probably easier to implement, involves a separate recordset for each group of sub-categories.

In the first method, you need to create a variable to keep track of the current category. Inside the first loop, display the current category, and then enter an inner loop to display the sub-categories, checking each time that the associated category still matches the value of the current category. If it doesn't, break out of the inner loop, display the next category, and go straight back into the inner loop, and so on.

Why am I not giving you the code? Because it's complex logic, that's why, and it would take me a long time to work it out. I have done it before, but it wasn't much fun.

The simpler way is to create a new recordset for the sub-categories in each category. Then just loop through each one in turn.

Votes

Translate

Translate

Report

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