Jump to content

Listing Items from a single column in database

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1
aakinn

aakinn

    Newbie

  • Members
  • PipPip
  • 24 posts
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

Frilixa Marao 60 Wall Site Dairy Multideck

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

Edited by Orjan, 22 July 2010 - 06:27 AM.
Please use code or php-tags when posting code


#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
use explode. It'll split the string up into an array.
Then loop trough the array, and for each item you can add <tr><td>item</td></tr> or anything else

info here: PHP explode() Function

example:
$features = "Stainless Steel, Fan cooler, Switchable Interior Light";
$featuresArray = explode(", " , $features);

$featuresArray[0] = Stainless Steel
$featuresArray[1] = Fan cooler
$featuresArray[2] = Switchable Interior Light

#3
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
the easiest part is to do this:

echo "<ul>";

$flist = explode(",", $row['features']);

foreach ($flist as $feat) {

  echo "<li>".$feat."</li>";

}

echo "</ul>";


__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#4
aakinn

aakinn

    Newbie

  • Members
  • PipPip
  • 24 posts
Cheers lads! Worked like a charm!

:thumbup1: