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
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>
");
}

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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!!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.