tags:

views:

49

answers:

1

Hey,

My problem seems complex to me, but im sure theres a simple way to do this.

Essentially, I have a treatments list. On the MYSQL table the list's items are broken up by catagory, title, description and price.

I need to echo out each catagory into a div each, sectioning them off. So, if the user has 4 catagories of items then I should have 4 divs with all the items from each catagory in each.

I cant call on a cataogory directly, because the catagory names are what ever the user sets them to.

Is there a way to do this? Possibly even a while inside a while with PHP?


@Kaaviar Table column names are in order - Catagory, Title, Description, Price

@sAc - This is the basic of what im using at the moment, but this just lists everything in order of Catagory.

  <?php

    include 'config.php';

    $treatments = mysql_query("SELECT * FROM treatments ORDER BY catagory") or die (mysql_error('woops') ); 

    while($list = mysql_fetch_array($treatments)) {

   echo $list['title'];
   echo $list['description'];
   echo $list['price'];


    } 

    ?>

@Luke - Your code returns nothing here on my end, with the necessary edits etc.

Thanks, Warren

+1  A: 

You can try the below, obviously you dont have to use the markup I have demo'ed.

$string = '<div><ul>';
$cat = '';
while ($row = mysql_fetch_assoc($result)) {
  if ($row['category'] != $cat && !empty($cat)) {
    $string .= '</ul></div><div><ul>';
  }
  $string .= '<li>' . $row['title'] . '</li>';
  $cat = $row['category'];
}
$string .= '</ul></div>';

Hope it helps

Luke

Luke