Results 1 to 3 of 3

Thread: Listing Items from a column in database

  1. #1
    Join Date
    Feb 2009
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Listing Items from a column in database

    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-equipm...deck/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

  2. #2
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

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

    PHP Code:
    $id $_GET['id'];
    settype($id"integer")
    $query mysql_query("SELECT * FROM tblproducts WHERE id='$id'") or die(mysql_error()); 
    Corrections to my coding/thoughts welcome.

  3. #3
    Join Date
    Jul 2010
    Location
    Minnesota
    Posts
    256
    Thanks
    1
    Thanked 21 Times in 21 Posts

    Default

    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.
    PHP Code:
    echo nl2br("$row['features']"); 
    Of course implement the nl2br into your code - this is just an example.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •