tags:

views:

81

answers:

1

What's this database design and it is displaying three rows of id in names in products and three rows of prices, and three row of each field when I am looking for to display the name, variety and price in one row then the next row display another different set of field values different than the first row. How can I achieve that with that with the database design below?

CREATE TABLE products (
id INT PRIMARY KEY NOT NULL AUTO_INCREMENT,
name VARCHAR(255)
);

CREATE TABLE product_varieties (
product_id INT,
variety VARCHAR(100),
price DOUBLE,
description TEXT,
PRIMARY KEY (product_id, variety)
);

INSERT INTO products (id, name) VALUES (1, 'Cotél de camarone');
INSERT INTO product_varieties (product_id, variety, price) VALUES (1, 'Small Tray',   2.9);
INSERT INTO product_varieties (product_id, variety, price) VALUES (1, 'Medium Tray', 6.9);
INSERT INTO product_varieties (product_id, variety, price) VALUES (1, 'Large Tray', 8.9);

Go here to see an example of what I am talking. Again I want the price 2.9 to display in small tray of the first row, then 6.9 display in the medium tray of the first row, and 8.9 display on large tray of the first row. Right now is display prices for row...

help.

+1  A: 

try converting your data into a tree-alike structure (array of arrays), which is much simpler to output in the "grouped" form

something like this (pseudocode)

 $tree = array();
 $sql = "select * from products
     left join product_varieties on product_varieties.product_id = products.id";
 $sth = query($sql);
 while($row = fetch($sth)) {
    $id = $row['id'];
    $tree[$id]['name'] = $row['name'];
    if($row['variety'])
       $tree[$id]['varieties'][] = $row;
 }

this builds an array like

   1 => array
      name => 'Cotél de camarone',
      varieties => array
          array(1, 'Small Tray', 2.9)
          array(1, 'Medium Tray', 6.9)
          etc
   2 => array
      name => 'Something else',
      varieties => array
          array(1, 'Small Tray', 2.9)
          array(1, 'Medium Tray', 6.9)
          etc

loop over this array and generate a html structures you need

foreach($tree as $product)
    <div>
    echo $product['name']
    foreach($product['varieties'] as $variety)
         etc etc
    </div>

hope i'm making sense

stereofrog
in the left join you meant to join WHERE product_varieties.product_id = id?I don't have a relational database product_id field in products I use id instead of product_id as the relational database field in between products and products.varieties tables.
jona
you were right is product.id sorry I understand that.
jona
Thank you it worked now after 3 hours is when I get to put it to work..Thnak you very much
jona
stereofrog, I have been trying to use the id generated in the while loop inside the form action url string inside the foreach parent as you use the name index in the partent foreach. I have tried echoing and printing some how it won't display the id or if I print it, then it will appear three rows of ids per rows like 111333444555, I just want to be able to have the id per row like echo $product['name'] does, each iteration only prints one row of the index [ 'name'].
jona
The form below won't echo anything, and I I change it to print then it will print 111222333444 help.. foreach($tree as $product)<form action="cart.php?id="'. echo $product['id']. '"></form> <div> echo $product['name'] echo $procude['id'] foreach($product['varieties'] as $variety) etc etc </div>
jona
it's better to ask a new question about this, jona. Post your data and code, and describe exactly what is not working. And accept some answers, 0% accept rate is not very motivating for people to answer you.
stereofrog
I don't know how to accept the answers That's something someone told me about but I don't know how to do it.
jona
hey Sterofrog I learned how to accept now Thank you
jona