How to save a cart

hi I got a cart where users can add products to it but when they logout the cart is empted

I want it to save they items they added to the cart so when they login the next day or from another computer it would still show the items in the cart. I’m just a beginner at php so please help:

here is the code for the cart.php

[php]<?php

session_start();
if (!isset($_SESSION[“manager”])) {
header(“location: user_login.php”);
exit();
}
// Be sure to check that this manager SESSION value is in fact in the database
$managerID = preg_replace(’#[^0-9]#i’, ‘’, $_SESSION[“id”]); // filter everything but numbers and letters
$manager = preg_replace(’#[^A-Za-z0-9]#i’, ‘’, $_SESSION[“manager”]); // filter everything but numbers and letters
$password = preg_replace(’#[^A-Za-z0-9]#i’, ‘’, $_SESSION[“password”]); // filter everything but numbers and letters
// Run mySQL query to be sure that this person is an admin and that their password session var equals the database information
// Connect to the MySQL database
include “…/storescripts/connect_to_mysql.php”;
$sql = mysql_query(“SELECT * FROM user WHERE id=’$managerID’ AND username=’$manager’ AND password=’$password’ LIMIT 1”); // query the person
// ------- MAKE SURE PERSON EXISTS IN DATABASE ---------
$existCount = mysql_num_rows($sql); // count the row nums
if ($existCount == 0) { // evaluate the count
echo “Your login session data is not on record in the database.”;
exit();
}
?>

<?php session_start(); // Start session first thing in script // Script Error Reporting error_reporting(E_ALL); ini_set('display_errors', '1'); // Connect to the MySQL database include "../storescripts/connect_to_mysql.php"; ?> <?php ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Section 1 (if user attempts to add something to the cart from the product page) ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// if (isset($_POST['pid'])) { $pid = $_POST['pid']; $wasFound = false; $i = 0; // If the cart session variable is not set or cart array is empty if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) { // RUN IF THE CART IS EMPTY OR NOT SET $_SESSION["cart_array"] = array(0 => array("item_id" => $pid, "quantity" => 1)); } else { // RUN IF THE CART HAS AT LEAST ONE ITEM IN IT foreach ($_SESSION["cart_array"] as $each_item) { $i++; while (list($key, $value) = each($each_item)) { if ($key == "item_id" && $value == $pid) { // That item is in cart already so let's adjust its quantity using array_splice() array_splice($_SESSION["cart_array"], $i-1, 1, array(array("item_id" => $pid, "quantity" => $each_item['quantity'] + 1))); $wasFound = true; } // close if condition } // close while loop } // close foreach loop if ($wasFound == false) { array_push($_SESSION["cart_array"], array("item_id" => $pid, "quantity" => 1)); } } header("location: cart.php"); exit(); } ?> <?php ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Section 2 (if user chooses to empty their shopping cart) ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// if (isset($_GET['cmd']) && $_GET['cmd'] == "emptycart") { unset($_SESSION["cart_array"]); } ?> <?php ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Section 3 (if user chooses to adjust item quantity) ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// if (isset($_POST['item_to_adjust']) && $_POST['item_to_adjust'] != "") { // execute some code $item_to_adjust = $_POST['item_to_adjust']; $quantity = $_POST['quantity']; $quantity = preg_replace('#[^0-9]#i', '', $quantity); // filter everything but numbers if ($quantity >= 100) { $quantity = 99; } if ($quantity < 1) { $quantity = 1; } if ($quantity == "") { $quantity = 1; } $i = 0; foreach ($_SESSION["cart_array"] as $each_item) { $i++; while (list($key, $value) = each($each_item)) { if ($key == "item_id" && $value == $item_to_adjust) { // That item is in cart already so let's adjust its quantity using array_splice() array_splice($_SESSION["cart_array"], $i-1, 1, array(array("item_id" => $item_to_adjust, "quantity" => $quantity))); } // close if condition } // close while loop } // close foreach loop } ?> <?php ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Section 4 (if user wants to remove an item from cart) ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// if (isset($_POST['index_to_remove']) && $_POST['index_to_remove'] != "") { // Access the array and run code to remove that array index $key_to_remove = $_POST['index_to_remove']; if (count($_SESSION["cart_array"]) <= 1) { unset($_SESSION["cart_array"]); } else { unset($_SESSION["cart_array"]["$key_to_remove"]); sort($_SESSION["cart_array"]); } } ?> <?php ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // 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 = "

Your shopping cart is empty

"; } else { // Start PayPal Checkout Button $pp_checkout_btn .= ' '; // 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 .= ' '; // Create the product array variable $product_id_array .= "$item_id-".$each_item['quantity'].","; // Dynamic table row assembly $cartOutput .= ""; $cartOutput .= ' ' . $product_name . '
' . $product_name. ' '; $cartOutput .= '' . $details . ''; $cartOutput .= '$' . $price . ''; $cartOutput .= ' '; //$cartOutput .= '' . $each_item['quantity'] . ''; $cartOutput .= '' . $pricetotal . ''; $cartOutput .= ''; $cartOutput .= ''; $i++; } setlocale(LC_MONETARY, "en_US"); $cartTotal = money_format("%10.2n", $cartTotal); $cartTotal = "
Cart Total : ".$cartTotal." USD
"; // Finish the Paypal Checkout Btn $pp_checkout_btn .= ' '; } ?> Your Cart
<?php include_once("template_header_user_login.php");?>
<br />
<table width="100%" border="1" cellspacing="0" cellpadding="6">
  <tr>
    <td width="18%" bgcolor="#C5DFFA"><strong>Product</strong></td>
    <td width="45%" bgcolor="#C5DFFA"><strong>Product Description</strong></td>
    <td width="10%" bgcolor="#C5DFFA"><strong>Unit Price</strong></td>
    <td width="9%" bgcolor="#C5DFFA"><strong>Quantity</strong></td>
    <td width="9%" bgcolor="#C5DFFA"><strong>Total</strong></td>
    <td width="9%" bgcolor="#C5DFFA"><strong>Remove</strong></td>
  </tr>
 <?php echo $cartOutput; ?>
 <!-- <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr> -->
</table>
<?php echo $cartTotal; ?>
<br />

<?php //echo $pp_checkout_btn; ?>

Click Here to Empty Your Shopping Cart

<?php include_once("template_footer_user_login.php");?>
[/php]

thanks

Well, even without looking at your code, there are many ways of saving a cart.
First, we would need some info to help you. Assuming you are posting your code online
and have a site setup somewhere… A couple ways to do this are:

Simplest way would be to use a database. Most PHP programmers use MySQL as their
database engine. It is fairly easy to use and quite secure.

Another way would be to store the data in files. This is more complicated, but, again fairly
easy to implement. It is less secure. You could save the data locally on their system, but, it
is not secure as they could change it.

So, if you are interested in doing this you should create a database and learn how to save
your data into it and how to retrieve it. If you need further help, you might google how to
start and ask questions here when you need it.

I’ll try the first option thanks for your help

Lahsoona,
We will close this thread as solved. Since you have a lot of designing and coding to do to save your cart and retrieve it back. When you get stuck on a section, post to this site with the problem in the subject and we will help you along. There are a lot of us that have experience in this area.
Also, do not fear Google. I use it all the time for code samples. Just google on something like “PHP how to update records to MySQL” or something similar. I always start it with the PHP for code or MySQL for queries, etc…
Good luck…

Sponsor our Newsletter | Privacy Policy | Terms of Service