Jump to content


Photo

PHP (& MySQL) display problems


  • Please log in to reply
2 replies to this topic

#1 nickholt1972

nickholt1972
  • Members
  • PipPipPip
  • Advanced Member
  • 30 posts
  • LocationBury, Lancs, UK

Posted 24 September 2006 - 04:56 PM

THE PROBLEM: I'm developing an online shop using PHP and MySQL. I've got products and each product might have different sizes, colours, etc
I have two tables: products and productdetails

When i list these in my shop I want it to say (e.g.)

Product 1:
Colour: white, Size: small
Colour white, Size: large

But what i'm getting is

Product 1:
Colour: white, Size: small

Product 1:
Colour white, Size: large

This is my SQL Query and the PHP to output it:

$result = @mysql_query('SELECT
  products.id, products.productid, products.productname,
  productdetails.id, productdetails.size
  FROM productdetails RIGHT JOIN products ON products.productid = productdetails.productid');

// Display the text of each entry in a table
  while ($row = mysql_fetch_array($result)) {
  echo '<div id="newproduct"><table border="1">'    .
  '<tr><td>' . $row['id'] . '</td><td>' .
  $row['productid']    . '</td><td>' .
  $row['productname']    . '</td></tr>' .
  '<tr><td>' . $row['size']    . '</td></tr></table></div>';
}

I'm pretty sure its not the MySQL query, I've also tried

FROM products, productdetails
WHERE products.productid = productdetails.productid

but it doesn't solve my problem

I'm sure its very simple, it usually is. Any help would be greatly appreciated.
Thanks,
Nick
Nick Holt - Future Guru

#2 onlyican

onlyican
  • Members
  • PipPipPip
  • Advanced Member
  • 921 posts
  • LocationHants - UK

Posted 24 September 2006 - 05:20 PM

I just built something which grabs details from the database, and each time the supplier is different, I wanted the supplier in a heading

So what I done is something like this

<?php
//Add on the end of your query
//ORDER BY productname



//then your while loop
while($row = ......){
if($prod_name != $row["product_name"]){
echo $row["product_name"]."<br />\n";
}
$prod_name = $row1["product_name"];
//Continue with your results


}


?>

Tell me the problem, I will try tell you the solution

#3 ronverdonk

ronverdonk
  • Members
  • PipPipPip
  • Advanced Member
  • 277 posts
  • LocationNetherlands

Posted 24 September 2006 - 05:25 PM

Maybe this is what you are loo0king for, I had to guess what each field is.
$oldid = "";
echo '<div id="newproduct"><table border="1">';
while ($row = mysql_fetch_array($result)) {
   if ($oldid != $row['productid']) {
      echo '<tr><td>' . $row['id']          . '</td>' .
               '<td>' . $row['productid']   . '</td>' .
	           '<td>' . $row['productname'] . '</td>' .
               '</tr>';
	          $oldid = $row['productid'];
   }
        '<tr><td>' . $row['size']        . '</td>' . 
        '</tr>';
}
echo '</table></div>';

Ronald  8)
RTFM is an almost extinct art form, it should be subsidized.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users