unable to solve this loop problem 2 days now. please help!

Discussion in 'PHP' started by zoreli, Dec 9, 2008.

  1. #1
    Hi

    The code bellow work, but not the way I want. It display my categories, my subcategories and my products.

    The code bellow, display this:

    category 1
    subcategory 1
    subcategory 2
    subcategory 1 product 1
    subcategory 1 product 2
    subcategory 2 product 1
    etc...

    What I want to achieve is this:

    category 1
    subcategory 1
    subcategory 1 product 1
    subcategory 1 product 2
    subcategory 2
    subcategory 2 product 1
    etc...

    Here is the code:

    
    <?php
    //get selected category
    
    $catidselected = $_GET['catid'];
    
    
    $query="SELECT * FROM products_categories where visible='Yes' order by sorder";
    
    $result=mysql_query($query);
    $num=mysql_num_rows($result);
    $h=0;
    
    while ($h< $num) {
    $catid=mysql_result($result,$h,"catid");
    $categoryname=mysql_result($result,$h,"categoryname");
    
    ?>
    <li class="cat"><a href="subcategories.php?catid=<?php echo $catid;?>"><?php echo $categoryname;?></a></li>
    
    
    <?php
    if ($catid ==$catidselected) {
    
    
    $query1="SELECT * FROM products_subcategories where catid = $catidselected and visible='Yes' order by sorder";
    
    $result1=mysql_query($query1);
    $num1=mysql_num_rows($result1);
    $h1=0;
    
    while ($h1< $num1) {
    $subcatid=mysql_result($result1,$h1,"subcatid");
    $subcategoryname=mysql_result($result1,$h1,"subcategoryname");
    
    
    ?>
    <li class="subcat"><a href="subcategories.php?subcatid=<?php echo $subcatid;?>"><?php echo $subcategoryname;?></a></li>
    <?php
    $h1++;
    };
    ?>
    
    
    <?php
    
    } else {
    
    };
    ?>
    <?php
    	
    if ($catid ==$catidselected) {
    
    
    $query1="SELECT * FROM products where catid = $catidselected and visible='Yes' order by sorder";
    
    $result1=mysql_query($query1);
    $num1=mysql_num_rows($result1);
    $h2=0;
    
    while ($h2< $num1) {
    $productid=mysql_result($result1,$h2,"productid");
    $productname=mysql_result($result1,$h2,"productname");
    
    
    
    ?>
    <li class="sub_subcat"><a href="productf.php?catid=<?php echo $catidselected;?>&amp;productid=<?php echo $productid;?>"><?php echo $productname;?></a></li>
    <?php
    $h2++;
    };
    
    ?>
    
    <?php
    } else {
    
    };
    
    $h++;
    };
    ?>
    
    PHP:
    Hopefully someone can easily see what I am doing wrong in this loop and help me to solve this problem on which I lost 2 days now...

    Regards, Zoreli
     
    zoreli, Dec 9, 2008 IP