Log in

View Full Version : Listing Items from a column in database



aakinn
07-22-2010, 01:49 PM
Hi all,

I have a slight problem, I want to be able to list items from a column in a database

The column in the database is called Features and basically contains a list of values seperated by commas ie Stainless Steel, Fan cooler, Switchable Interior Light

Now I want to be able to display these in a list of features much like the link below

http://www.advantage-catering-equipment.co.uk/products/refrigeration/multideck-refrigeration/stainless-steel-multideck/frilixa-marao-60-wall-site-dairy-multideck/203496999

Here is what I have so far

<?php
require 'includes/mysql.php';
$connection = mysql_connect ($host, $user, $passwd) or die ('Error connecting to mysql'); // connection to our mysql server
mysql_select_db ($dbName) or die ('Error connecting to selected database'); //connection to our selected database

//display full product
$id = $_GET['id'];
$query = mysql_query("SELECT * FROM tblproducts WHERE id='$id'") or die(mysql_error());
while($row=mysql_fetch_assoc($query)) {
//format the output
echo '<h1>'.$row['name'].'</h1>
<br />
<hr />
<br />
<table class="center">
<tr>
<td><img src="image.php?id='.$row['id'].'"></td>
<td align>Name:<br /> '.$row['name'].'</td>
<td align>Price:<br /> £'.$row['price'].'</td>
<td align>Features:<br /> '.$row['features'].'</td>
</tr>
</table>';
}


?>

Now currently as you can imagine this just prints out the features column all on one line, how am I able to produce a list out of the items in the features field which have been seperated by a comma.

Any help would be much appreciated!

Thanks
Akin

bluewalrus
07-22-2010, 02:08 PM
Can you make a sample of a "features" entry? You also shouldn't just take an input like you are doing for the id. If this is always a numerical value...



$id = $_GET['id'];
settype($id, "integer")
$query = mysql_query("SELECT * FROM tblproducts WHERE id='$id'") or die(mysql_error());

fastsol1
07-22-2010, 02:22 PM
Someone may have a better answer but this is what I have done. Instead of sperating with commas, delete the commas and hit enter to place the next feature on a new line. This works best I think if the structure is set as a TEXT field rather than a VARCHAR. Then in your echo statement use the nl2br() and it will echo out the line breaks.

echo nl2br("$row['features']");
Of course implement the nl2br into your code - this is just an example.