user7923788
user7923788

Reputation:

What is the best method for saving shopping cart orders to a database? Saving the items as an array in one row or in multiple rows?

Just looking for advice here. I have a shopping cart website. I will post my php that receives the items and prints them out. However, I am looking to store these orders in my database. Should I... Can I... save multiple items under one row, in one cell like this?

{"114":{"id":"114","name":"Raskals Lemon","quantity":1,"price":15,"weight":1,"shopid":"1","shopname":"MMD","type":"Sativa","size":"---","strain":"Sativa","thc":"---","subtype":"Flower"},"117":{"id":"117","name":"Nectarine","quantity":1,"price":15,"weight":1,"shopid":"1","shopname":"MMD","type":"Sativa","size":"---","strain":"Sativa","thc":"---","subtype":"Flower "}}

Or would it be better to save all the items and there details as multiple rows and use something like a 'order number' to tie the rows together in future queries?

Here is the basic php, if it pertains at all to the question.

$order = '';

  for($i=1; $i < $content['itemCount'] + 1; $i++) {

    $item_name = 'item_name_'.$i;
    $item_quantity =  'item_quantity_'.$i;
    $item_price = 'item_price_'.$i;
    $item_weight = 'item_weight_'.$i;
    $item_id = 'item_id_'.$i;
    $item_shop = 'item_shopid_'.$i;
    $item_type = 'item_type_'.$i;  
    $item_shopname = 'item_shopname_'.$i;  
    $item_size = 'item_size_'.$i;  
    $item_strain = 'item_strain_'.$i; 
    $item_subtype = 'item_subtype_'.$i; 






$order .= <<<EOT



    <li>

        <h2 style="font-size:14px;">$content[$item_name]</h2>
        <p style="text-indent:5px;padding-top:5px;"><strong>Quantity:</strong> $content[$item_quantity]</p> 
        <p style="text-indent:5px;"><strong>Grams:</strong> $content[$item_weight]</p>
        <p style="text-indent:5px;"><strong>Catagory:</strong> $content[$item_type]</p> 
        <p style="text-indent:5px;"><strong>Sub Catagory:</strong> $content[$item_subtype]</p>      
        <p style="text-indent:5px;"><strong>Size:</strong> $content[$item_size]</p>     
        <p style="text-indent:5px;"><strong>Strain:</strong> $content[$item_strain]</p>




            <p class="ui-li-aside"><strong>$currency$content[$item_price].00</strong></p>
            <span class="ui-li-count">$i</span>

    </li>








EOT;
}

Upvotes: 0

Views: 2305

Answers (2)

Albert221
Albert221

Reputation: 7092

I see two tables here (third one is what you already have, I assume):

tables structure

  • order table is a place where you store every orders. One order = one record in that table. You can have some order-related details here, like delivery address, delivery method etc.
  • order_item table is a place where you store each ordered item. This has relation of many to one. Many ordered items in one order. You have item-releated details here like quantity of given product, size and other stuff.

Upvotes: 2

Pritam Pan
Pritam Pan

Reputation: 133

from a real world understanding there is a concept of order header and order lines. So you can design your underlying tables in two different tables with different granularity. For e.g. Order header table - should contain only the parent information like order number, customer number, order date etc.

Order Line Item table - should contain all the child items, for each order number there could be 1 or more rows with information like order number, item number, quantity etc.

Hope you find this useful.Thanks

Upvotes: 0

Related Questions