Jaymin Sejpal Jaymin Sejpal - 1 year ago 59
PHP Question

How to fetch my cart session values from one page to another?

I am building a simple cart, now my next requirement is to post cart session values to placeorder page.
The place order page should contain cart values as well as delivery information.
Here is my code:

$cartOutput = "";
$cartTotal = "";
$pp_checkout_btn = '';
$product_id_array = '';
if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) {
$cartOutput = "<h2 align='center'>Your shopping cart is empty</h2>";
} else {
// Start PayPal Checkout Button
//$pp_checkout_btn .= '<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
//<input type="hidden" name="cmd" value="_cart">
//<input type="hidden" name="upload" value="1">
//<input type="hidden" name="business" value="you@youremail.com">';
// Start the For Each loop
$i = 0;
foreach ($_SESSION["cart_array"] as $each_item) {
$item_id = $each_item['item_id'];
$sql = mysql_query("SELECT * FROM products WHERE id='$item_id' LIMIT 1");
while ($row = mysql_fetch_array($sql)) {
$product_name = $row["product_name"];
$price = $row["price"];
$details = $row["details"];
$pricetotal = $price * $each_item['quantity'];
$cartTotal = $pricetotal + $cartTotal;
//setlocale(LC_MONETARY, "en_US");
//$pricetotal = money_format("%10.2n", $pricetotal);
// Dynamic Checkout Btn Assembly
$x = $i + 1;
$pp_checkout_btn .= '<input type="hidden" name="item_name_' . $x . '" value="' . $product_name . '">
<input type="hidden" name="amount_' . $x . '" value="' . $price . '">
<input type="hidden" name="quantity_' . $x . '" value="' . $each_item['quantity'] . '"> ';
// Create the product array variable
$product_id_array .= "$item_id-"."$product_name".$each_item['quantity'].",";

// Dynamic table row assembly
$cartOutput .= "<tr>";
$cartOutput .= '<td><a href="product.php?id=' . $item_id . '">' . $product_name . '</a><br /><img src="inventory_images/' . $item_id . '.jpg" alt="' . $product_name. '" width="40" height="52" border="1" /></td>';
$cartOutput .= '<td>' . $details . '</td>';
$cartOutput .= '<td>Rs ' . $price . '</td>';
$cartOutput .= '<td><form action="cart.php" method="post">
<input name="quantity" onkeypress="return onlyNos(event,this);" type="text" value="' . $each_item['quantity'] . '" size="1" maxlength="2" />
<input name="adjustBtn' . $item_id . '" type="submit" value="change" />
<input name="item_to_adjust" type="hidden" value="' . $item_id . '" />
//$cartOutput .= '<td>' . $each_item['quantity'] . '</td>';
$cartOutput .= '<td>' . $pricetotal . '</td>';
$cartOutput .= '<td><form action="cart.php" method="post"><input name="deleteBtn' . $item_id . '" type="submit" value="X" /><input name="index_to_remove" type="hidden" value="' . $i . '" /></form></td> ';
$cartOutput .= '</tr>';

Now this is my cart array which I use to render it on my cart page.
Now how can i fetch values from cart array.

Answer Source

I don't know whether you created something regarding orders, but usually, you first need to create db tables and relations. For example, you will have table order and also will have table order_item. Relation between them is one-to-many, and that means that order could have many items and one item could have only one order.

Beside that, you have products in you system, so I guess you created product table already. You can also make relation between product and order_item as well. Relation is again one-to-many, an item could have one product and product could have many items.

So, you db should be something like:

order table:

order_item table:

One very good practice is when you create order_item table, to put all data from product into order_item table like name, price, tax, ....

This is important because you need that data to be historical, otherwise you could lose the price for example. So your order_item table might look:

order_item table:

Let's assume that you have only product_id as foreign key in order_item, because you might think "Ok, when I need to check item from one order and need to see the price, I can get it by foreign key and grab from product table". But what will happen if you change price in product table? Then you might lost for forever price which was in that time when order was created.

This would be the beginning, and afterwards you can pretty easily fetch with sql query all order information from order and order_item tables

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download