Code Help

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
Diddy74
Forum Newbie
Posts: 2
Joined: Wed Nov 12, 2008 11:20 am

Code Help

Post by Diddy74 »

Hi

I need some help with this code........


$result = mysql_query("SELECT Distinct SubProduct, Product FROM Table Order by PRODUCT");
while($row = mysql_fetch_array($result))
{

echo " " .$row['Product']. " ";
echo " " .$row['SubProduct']. " ";
}

This gives me the following output

Products 1 SubProduct 1
Products 1 SubProduct 2
Products 1 SubProduct 3
Products 1 SubProduct 4

Products 2 SubProduct 1
Products 2 SubProduct 2
Products 2 SubProduct 3
Products 2 SubProduct 4


However I would like to display the information this way with the Product name only displaying once like this

Product 1
SubProduct 1 SubProduct 2 SubProduct 3 SubProduct 4


Product 2
SubProduct 1 SubProduct 2 SubProduct 3 SubProduct 4

Can anoyone show me what I need to do the the code to get my information to display the way I want it?
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Re: Code Help

Post by infolock »

A quick and dirty fix? try this

Code: Select all

 
$result = mysql_query("SELECT Distinct SubProduct, Product FROM Table Order by PRODUCT");
 
$product_name = "";
 
while($row = mysql_fetch_array($result)) {
  if($product_name != $row['Product']) {
    $product_name = $row['Product'];
    echo "<h3>$product_name</h3>";
  }
  echo "{$row['SubProduct']} ";
}
 
 
Diddy74
Forum Newbie
Posts: 2
Joined: Wed Nov 12, 2008 11:20 am

Re: Code Help

Post by Diddy74 »

infolock thanks very much, exactly what I needed.

Cheers!!!
Post Reply