0

I am attempting to implement a shopping cart using php, and html. The problem I am having is storing the id of the product to be stored using a session. The following is the code that I have at the moment:

<?php
session_start();
?>
//There are three HTML Forms that take and display input, and output.

<?php
$_SESSION['cart'] = array();

if(isset($_GET['search'])){
  echo "<table border=1>";
  echo "<th>Product Image</th>";
  echo "<th>Product Name</th>";
  echo "<th>Price</th>";

  foreach($xml->categories->category->items->product as $product){
    $imageURL = $product->images->image[0]->sourceURL;
    $id = $product['id'];
    echo "<tr>";
    echo "<td><a href= 'buy.php?buy=".$id."'><img src=".$imageURL."></img></a></td>";
    echo "<td>".$product->name."</td>";
    echo "<td>".'$'.$product->minPrice."</td>";
  }
}

if(isset($_GET['buy'])){
  $product_id = $_GET['buy'];
  if(isset($_SESSION['cart'])){
    array_push($_SESSION['cart'],$product_id);
  }
}

print_r ($_SESSION);
?>

What the first if statement does is that it gets the search word, and gets the closest results. It then displays an image, name, and price. When you click on the image, there is a href and it is supposed to be added to the shopping cart. That is where the second if statement comes into play. If the image has been clicked I want to get the id and store it in session. It stores the id of the product when clicked upon, but when I go back to add another item the previous id is replaced with the new id. Could anybody explain to me where I went wrong? Any help would be greatly appreciated.

1
  • Check the session cart before assigning to empty array. Check my code below. Is it the solution working for you? Commented Nov 2, 2013 at 23:05

5 Answers 5

2

Your problem is in this line: $_SESSION['cart'] = array();

check like this:

if(empty($_SESSION['cart']))
  $_SESSION['cart'] = array();

That should solve it

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

Comments

1

First you have to assign an array to your $_SESSION['cart']. Otherwise it will store only a single value.

$ids = array();

$_SESSION['cart'] = $ids;

Then it will work.

1 Comment

I tried doing this, but the first value is still being replaced by the values after it.
0

Can you please try this,

          if(isset($_GET['buy'])){
                $product_id = $_GET['buy'];       
                  if(!in_array($product_id, $_SESSION['cart'])){
                      $_SESSION['cart'][]=$product_id;
                  }             
             }

1 Comment

Thank you for the response, however, it still replaces the old value with the new value instead of accumulating them.
0

You must use array to store multiple values:

$shop_array= array(); // this is create the array with the name shop_array
// now create the session and assign the shop_array to creating session
$_session['mycart'] = $shop_array;

You can check the stored values using print_r() function:

print_r($_session['mycart']);

You will get the result set of stored ID's.

1 Comment

Thank you for the suggestion, but I was already aware of it. That is the reason for me knowing that the array elements are being replaced. This was the same suggestion by someone else, and does not seem to be working for me.
0

you have to options to store cart - sessions and cookies i personally prefer sessions try this code for storing cart in sessions

if(isset($_POST['item_src']))
{
  $_SESSION['name'][]=$_POST['item_name'];
  $_SESSION['price'][]=$_POST['item_price'];
  $_SESSION['src'][]=$_POST['item_src'];
  echo count($_SESSION['name']);
  exit();
}

complete tutorial for creating add to cart system here http://talkerscode.com/webtricks/simple-add-to-cart-system-using-jquery-ajax-and-php.php

Comments

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.