Loop problem - categories and sub categories

Hi Folks,

I’m trying to display the following html from my query:

 
<h2>Aluminium</h2>
<ul>
  <li>Angles11Aluminium</li>
</ul>
<li>Flat Bar11Aluminium</li>
<li>Round Bar11Aluminium</li>
<li>Square Bar11Aluminium</li>
<li>Box Section11Aluminium</li>
<li>Channel11Aluminium</li>
<li>Chequer Plate5 Bar Chequer Plate.jpg11Aluminium</li>
<li>Round Tube11Aluminium</li>
<li>Square Tube11Aluminium</li>
<h2>Stainless Steel</h2>
<ul>
  <li>Flat Bar44Stainless Steel</li>
</ul>
<li>Round Bar44Stainless Steel</li>
<li>Square Bar44Stainless Steel</li>
<li>Polished Sheet44Stainless Steel</li>
<li>Plain Sheet44Stainless Steel</li>
<h2>Plastic</h2>
<ul>
  <li>Acetal Round Bar33Plastic</li>
</ul>
<li>End Caps for Tubes33Plastic</li>
<h2>Brass</h2>
<ul>
  <li>Flat Bar22Brass</li>
</ul>
<li>Round Bar22Brass</li>
<li>Square Bar22Brass</li>
<li>Sheet22Brass</li>


However It closes the UL tag after the first sub item, can anyone help me output it properly?

Loop :


$query="SELECT *
FROM productCat
INNER JOIN productType ON productCat.productCattype = productType.productTypeId ORDER BY catid";
    $result = mysql_query($query);

while($row = mysql_fetch_array($result)){


if ($cur_catid != $row['productTypeId']){ //first record in this category
echo "<h2>" . $row['productTypeName'] . "</h2><ul>";
}



echo "<li>" .  $row['CatName']  . $row['catImage'] . $row['productCatType'] . $row['productTypeId'] . $row['productTypeName'] . "</li>";



if ($cur_catid != $row['productCatType']){ //get last
echo "</ul>";
}


$cur_catid = $row['productTypeId']; 



} 

SQL table:

catID CatName productCatType productTypeId productTypeName
1 Angles 1 1 Aluminium
2 Flat Bar 1 1 Aluminium
3 Round Bar 1 1 Aluminium
4 Square Bar 1 1 Aluminium
5 Box Section 1 1 Aluminium
6 Channel 1 1 Aluminium
7 Chequer Plate 1 1 Aluminium
8 Round Tube 1 1 Aluminium
9 Square Tube 1 1 Aluminium
10 Flat Bar 4 4 Stainless Steel
11 Round Bar 4 4 Stainless Steel
12 Square Bar 4 4 Stainless Steel
13 Polished Sheet 4 4 Stainless Steel
14 Plain Sheet 4 4 Stainless Steel
15 Acetal Round Bar 3 3 Plastic
16 End Caps for Tubes 3 3 Plastic
17 Flat Bar 2 2 Brass
18 Round Bar 2 2 Brass
19 Square Bar 2 2 Brass
20 Sheet 2 2 Brass

Thanks for your help much appreciated


$query = "
  SELECT *
  FROM productCat
  INNER JOIN productType
  ON productCat.productCattype = productType.productTypeId
  ORDER BY catid
";
$result = mysql_query($query);

// let's initialize this variable, so we can use it to decide if there is a ul to be closed 
$cur_catid = '';

while ($row = mysql_fetch_array($result)) {
  if ($cur_catid != $row['productTypeId']) { //first record in this category
    if ($cur_catid != '') { // we're already elaborating a category, so we have to close the current ul before starting the new category
      echo "</ul>";
    }
    echo "<h2>" . $row['productTypeName'] . "</h2><ul>";
    $cur_catid = $row['productTypeId'];
  }
 
  echo "<li>" .  $row['CatName']  . $row['catImage'] . $row['productCatType'] . $row['productTypeId'] . $row['productTypeName'] . "</li>";
}
if ($cur_catid != '') { // we've displayed a category, so we have to close the last ul
  echo "</ul>";
}

Thanks very much for your reply.

Do you know how I can tell if its at the end of the loop so I can close the ul?

Thanks

!= means ‘not equal’
So the code in that if will always execute when a new category is found, except for the first time.

Thanks very much for your reply.

I can’t quite see how that is going to work. Could you please explain a bit more?

Using my query results in my first post, productTypeId is only ever 1,2,3 or 4.

So $cur_catid will never be ’ ’

Therefore this code will never execute


if ($cur_catid != '') { // we're already elaborating a category, so we have to close the current ul before starting the new category
      echo "</ul>";
    }



Thanks again

Let’s indent the code a bit to make it more readable


$query = "
  SELECT *
  FROM productCat
  INNER JOIN productType 
  ON productCat.productCattype = productType.productTypeId
  ORDER BY catid
";
$result = mysql_query($query);

while ($row = mysql_fetch_array($result)) {
  if ($cur_catid != $row['productTypeId']) { //first record in this category
    echo "<h2>" . $row['productTypeName'] . "</h2><ul>";
  }

  echo "<li>" .  $row['CatName']  . $row['catImage'] . $row['productCatType'] . $row['productTypeId'] . $row['productTypeName'] . "</li>";

  if ($cur_catid != $row['productCatType']) { //get last
    echo "</ul>";
  }

  $cur_catid = $row['productTypeId']; 
} 

It opens and closes the ul in the same loop, because you set $cur_catid at the end of the loop. So when it’s not equal in the first if, it’s not equal in the second if as well :slight_smile: