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.