0

i have this session variable that hold an array which stores the current products in the cart,after a customer has choose from the list of product.

this is the array.

echo $_SESSION['cart'];

OUTPUT:
Array (
 [5] => Array ( [quantity] => 1 [price] => 45.00 )

 [7] => Array ( [quantity] => 1 [price] => 18.00 ) 
 ) 1

this array shows that there are two items in the cart with products id's 5 and 7 receptively with there quantities ordered and the price of the product.

I would want to save this array in table where I can save and recollect this info. thank you

3
  • You could always serialize the array into a specific column in a table, but I wouldn't recommend it. I think what you're trying to is more a databasedesign-related issue. I would have created a table called like orders and put the data into that table (and have column for price, a column for quantity etc). That would be more flexible and is a far better approach in my opinion. Commented Apr 26, 2014 at 16:20
  • could you help a little more by explaining your method please Commented Apr 26, 2014 at 17:30
  • I have posted an answer just for explaining my approach. Commented Apr 27, 2014 at 9:25

3 Answers 3

2

use serialize

$varible = serialize($_SESSION['cart']);

And store it in a VARCHAR TYPE

And When you returning data use unserialze

Sign up to request clarification or add additional context in comments.

1 Comment

thanks very much it work for me but one last problem how wud i be able to take out the values out the array to do some calculation. example inoder to get my subtotal for each product i have to multiply the quantity by the price . and the in order to get the grand total i have to add all the subtotals to get the grand total.
0
$cart = serialize($_SESSION['cart']);

This should work ;)

1 Comment

That will not store it in the database.
0

I wouldn't really store all values for the product in array in a column in a table.

I would create a table to store orders made.

order-table:

orders

order_id (primary key)
product_id (reference to which product it is in products-table)
quantity
price (price when purchased the product)

Based on the session you have:

Array (
 [5] => Array ( [quantity] => 1 [price] => 45.00 )

 [7] => Array ( [quantity] => 1 [price] => 18.00 ) 
 )

If 5 and 7 is your actual productids, then you should store the values something like this in orders-table:

INSERT INTO orders (product_id, quantity, price) VALUES (5, 1, 45000);
INSERT INTO orders (product_id, quantity, price) VALUES (7, 1, 18000)

If you have a product that costs 45.50 you should store it like this:

INSERT INTO orders (product_id, quantity, price) VALUES (5, 1, 45500);

The reason why this is a better approach is that is far more easy to extract information about any order later on. (for example to return all sales for a specific product)

If you then want to get the total for a specific order, just do a SQL:

SELECT SUM(price) FROM orders WHERE order_id = 14 (example for order 14)

If you want to return all values for the order, you just do like:

SELECT * FROM orders WHERE order_id = 14 (example for order 14)

2 Comments

okay initial that was my target, so i decided to take out the values using implode from the array but i wasnt getting the results i was expecting that why i decided to store the whole array. if there is a way to take out the values from the array so i can stored them like how you are explaining please teach me
It's just to use some basic SQL. I've updated my question with an example

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.