Adding <li> <ul> tags to loops

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Addos
Forum Contributor
Posts: 305
Joined: Mon Jan 17, 2005 4:13 pm

Adding <li> <ul> tags to loops

Post by Addos »

edited
Last edited by Addos on Tue Aug 12, 2008 1:52 pm, edited 1 time in total.
www.WeAnswer.IT
Forum Newbie
Posts: 24
Joined: Wed Mar 19, 2008 6:33 pm

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

Post 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.
Addos
Forum Contributor
Posts: 305
Joined: Mon Jan 17, 2005 4:13 pm

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

Post by Addos »

edited
Last edited by Addos on Tue Aug 12, 2008 1:53 pm, edited 1 time in total.
Addos
Forum Contributor
Posts: 305
Joined: Mon Jan 17, 2005 4:13 pm

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

Post by Addos »

edied
Last edited by Addos on Tue Aug 12, 2008 1:53 pm, edited 1 time in total.
www.WeAnswer.IT
Forum Newbie
Posts: 24
Joined: Wed Mar 19, 2008 6:33 pm

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

Post 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.
Post Reply