Jump to content

Displaying products from a db


stualk

Recommended Posts

I'm trying to work out how best to display products from a db but i'm having difficulty with my select statement. Here's an example of part of the db:

 

id product  fabric    colour

1  square    leather  cream

2  square    leather  black

3  square    leather  white

4  square    suede    cream

5  round    leather  black

6  round    leather  white

7  round    suede    cream

8  round    suede    white

 

What I want to do is for someone to be able to view the page containing the square products, the select statement then calls out the first fabric option (leather) and then displays the colour options beneath, like this

 

Square products:

 

Leather

Available in: cream | black | white

 

Suede

Available in: cream

 

Am I going to need an array here or is there an easier way to do this?

Link to comment
https://forums.phpfreaks.com/topic/140054-displaying-products-from-a-db/
Share on other sites

Thanks - I'm not certain. Take a look at the code I've been using. I've already created the table, it's just displaying the results i'm having difficulty with:

 

require ('database.php');

$query2 = mysql_query("select DISTINCT fabric from TABLE where product = '$product'");
while($rst = mysql_fetch_array($query2)) {

echo("
<tr>
<td class='body'>
<b>$rst[fabric]</b><br>Available in: $rst[colour]
</td>
</tr>
");
}

here is your code cleaned up a bit

 

<?php require ('database.php');

$query2 = mysql_query("select DISTINCT fabric, colour from TABLE where product = '$product'")or die(mysql_error());

while($rst = mysql_fetch_array($query2)) {
echo '<tr>';
echo '<td class="body">'
	echo '<b>' . $rst['fabric'] . '</b><br>Available in: ' .  $rst['colour'];
echo '</td>';
echo '</tr>';
}
?>

 

**made a few edits after re reading my code

That's fine, cheers.

This is almost working how I want it to. However, using the code above it displays the products like this:

 

Leather

Available in: Black

Leather

Available in: Cream

Leather

Available in: White

 

Ideally I want it to display like this:

Leather

Available in: Black, Cream, White

 

How can I best achieve this? Please don't say it's an array as I can never get my head around them!!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.