Page 1 of 1

Adding <li> <ul> tags to loops

Posted: Sun Mar 23, 2008 3:58 pm
by Addos
edited

Re: Adding <li> <ul> tags to loops

Posted: Sun Mar 23, 2008 4:15 pm
by www.WeAnswer.IT
You were pretty close. Try this out:

Code: Select all

<ul>
 
<?php
 
  $output = '';
 
  mysql_select_db($database_*****, $*****);
 
  $xHeadings = mysql_query("SELECT *
   FROM tbl_prdtcategories
   WHERE category_archive = 0 ");
 
  while($xHeading = mysql_fetch_assoc($xHeadings)) {
    $output .=  '<li>'.'<a href="results.php?category=' . $xHeading['category_ID'] . '">' . $xHeading['category_Name'] .'</a><ul>';
 
   $xProducts = mysql_query("SELECT  *
     FROM tbl_prdtcategories, tbl_products,tbl_prdtcat_rel
     WHERE product_ID = prdt_cat_rel_Product_ID
     AND product_Archive = 0
     AND product_OnWeb = 1
     AND category_ID = prdt_cat_rel_Cat_ID
     AND category_Name = '$xHeading[category_Name]'
     ORDER BY product_Name");
 
    while($xProduct = mysql_fetch_assoc($xProducts)) {
      $output .= '<li>'.'<a href="details.php?prodId=' . $xProduct['product_ID'] . '">' . $xProduct['product_Name'] .'</a>' .'</li>';
     }
      $output .= '</ul></li>';
  }
print $output;
 ?>
 </ul>



Also, in the last chunk of code you posted, these lines had an error:

Code: Select all

$xProducts = mysql_fetch_row($xProducts);
     
     if($xProducts)
     {
        $output[] = "<li><a href=\"details.php?prodId='{$xProduct['product_ID']}\">{$xProduct['product_Name']}</a></li>";
     }
The part that says $xProducts = mysql_fetch_row($xProducts); should say $xProduct = mysql_fetch_row($xProducts);. There are some other issues with that code, such as minimizing the number of queries that the script imposes on the database, but the code I posted above should be alright for your purposes.

Re: Adding <li> <ul> tags to loops

Posted: Sun Mar 23, 2008 5:26 pm
by Addos
edited

Re: Adding <li> <ul> tags to loops

Posted: Sun Mar 23, 2008 5:43 pm
by Addos
edied

Re: Adding <li> <ul> tags to loops

Posted: Mon Mar 24, 2008 2:04 pm
by www.WeAnswer.IT
Ok, try this out. I have cleaned it up a little further so that it's easier to read the output.

Code: Select all

<ul>
<?php
  $output = '';
  mysql_select_db($database_*****, $*****);
 
  $xHeadings = mysql_query("SELECT *
                           FROM tbl_prdtcategories
                           WHERE category_archive = 0 "
                           );
 
  while($xHeading = mysql_fetch_assoc($xHeadings)) {
    $output .=  "\n\t<li>\n\t\t".'<a href="results.php?category=' . $xHeading['category_ID'] . '">' . $xHeading['category_Name'] .'</a>';
 
   $xProducts = mysql_query("SELECT  *
                            FROM tbl_prdtcategories, tbl_products,t bl_prdtcat_rel
                            WHERE product_ID = prdt_cat_rel_Product_ID
                            AND product_Archive = 0
                            AND product_OnWeb = 1
                            AND category_ID = prdt_cat_rel_Cat_ID
                            AND category_Name = '{$xHeading['category_Name']}'
                            ORDER BY product_Name"
                            );
    
    if(mysql_num_rows($xProducts) > 0)
    {
        $output .= "\n\t\t<ul>";
        while($xProduct = mysql_fetch_assoc($xProducts)) {
            $output .= "\n\t\t\t<li>".'<a href="details.php?prodId=' . $xProduct['product_ID'] . '">' . $xProduct['product_Name'] .'</a>' .'</li>';
        }
        $output .= "\n\t\t</ul>\n\t</li>";
    }
  }
print $output;
?>
</ul>

Unfortunately I am not at my usual computer and I can't test it out for you.