www.webdeveloper.com
Results 1 to 3 of 3

Thread: Category / sub-category loop for drop down menu

  1. #1
    Join Date
    Dec 2011
    Posts
    23

    Category / sub-category loop for drop down menu

    I'm trying to dynamically populate a drop down menu with categories and subcategories from a database table. My problem is that I can't figure out a way to loop through the subcategories so they display under the correct category.

    In my SQL database I have a table called 'Categories' with 3 columns: id, category, subcategory. The subcategories in the subcategory column are separated by 3 colon marks (Artists:::Childcare:::Classes:::Event).

    I'm using unordered lists <Ul> <li> in html to populate the drop down menu.

    This code displays the category names:
    Code:
    $reqr1="select * from $categoryTable";
    $resultre1=mysql_query($reqr1);	
    
    
    	<?php 
    while($allCategories=mysql_fetch_assoc($resultre1)){ ?>
    <li><a href="#"><div class="whatever"<?php print $relanguage_tags[$allCategories['category']];?>' <?php if(in_array($relanguage_tags[$allCategories['category']],$reCategory)) print "selected='selected'"; ?> ><?php print $relanguage_tags[$allCategories['category']]; ?></div></a>
    <?php } ?>

    This code loops through the subcategories but it is a work in progress
    Code:
                  <?php 
    if($ptype=="showOnMap" || $ptype=="viewFullListing" || $ptype=="home" || $ptype==""){
    	$reCategoryString=getCommaStringFromArray($reCategory);
    	$reqr2="select * from $categoryTable where id like '%' ".getRealValue($reCategoryString,"reCategory");
    	$resultre2=mysql_query($reqr2);
    	?>
    	
    	<?php 
    	while($allSubCategories=mysql_fetch_assoc($resultre2)){ 
    	$subCatList=explode(":::",$allSubCategories['subcategories']);
    	$subCatSize=sizeof($subCatList);
    	for($i=0;$i<$subCatSize;$i++){
    		?>
    	<li><a href="#"><div class="whatever"<?php print $relanguage_tags[$subCatList[$i]];?>' <?php if(in_array($relanguage_tags[$subCatList[$i]],$reSubcategory)) print "selected='selected'"; ?> ><?php print $relanguage_tags[$subCatList[$i]]; ?></div></a></li>
    	<?php }
    	} ?>
    	
    <?php 	
    }
    ?>

  2. #2
    Join Date
    Jan 2008
    Location
    Florida
    Posts
    1,227
    What you need is a recursive query. Do a search for that.

  3. #3
    Join Date
    Feb 2012
    Location
    youTUBE
    Posts
    234

    I am trying to follow!

    When you say dynamically populate the category do you mean fetch the subcategory data when the category is clicked, or mouseovered? Or do you mean populate the entire menu structure all at once?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles