Jaymin Sejpal Jaymin Sejpal - 3 months ago 20x
PHP Question

Fetching Values from cart to another page

I have stored all cart values into an array.Now I wants to display it on another page? Here is my code for cart.

// Section 5 (render the cart for the user to view on the page)
$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-".$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>';
setlocale(LC_MONETARY, "en_US");
$cartTotal = money_format("%10.2n", $cartTotal);
$cartTotal = "<div style='font-size:18px; margin-top:12px;' align='right'>Cart Total : ".$cartTotal." </div>";
// Finish the Paypal Checkout Btn
/*$pp_checkout_btn .= '<input type="hidden" name="custom" value="' . $product_id_array . '">
<input type="hidden" name="notify_url" value="https://www.yoursite.com/storescripts/my_ipn.php">
<input type="hidden" name="return" value="https://www.yoursite.com/checkout_complete.php">
<input type="hidden" name="rm" value="2">
<input type="hidden" name="cbt" value="Return to The Store">
<input type="hidden" name="cancel_return" value="https://www.yoursite.com/paypal_cancel.php">
<input type="hidden" name="lc" value="US">
<input type="hidden" name="currency_code" value="USD">
<input type="image" src="http://www.paypal.com/en_US/i/btn/x-click-but01.gif" name="submit" alt="Make payments with PayPal - its fast, free and secure!">*/
echo "</form>'";

The above code is for storing cart values into an array. Now I want all of these information from cart page to another page. This all values are already stored in session. As You can see by this:


So how can I display that all the information on another page carrying all those values. If I simply include this:

include 'cart.php'

It includes all the properties and values of that page. I want only cartTotal, productname and product details.


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