user2659117
user2659117

Reputation: 17

Adding item to shopping cart using php and html

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.

Upvotes: 0

Views: 21883

Answers (5)

naresh
naresh

Reputation: 41

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

Upvotes: 0

lessoncup
lessoncup

Reputation: 57

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.

Upvotes: 0

kidz
kidz

Reputation: 308

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

check like this:

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

That should solve it

Upvotes: 2

Krish R
Krish R

Reputation: 22711

Can you please try this,

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

Upvotes: 0

Dilantha
Dilantha

Reputation: 1634

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.

Upvotes: 1

Related Questions