This is very barebone cart, you will easily be able to adapt it to your needs.
Ive tested it and it does work.
heres the search page (name not that important for this example) which loops and puts the add buttons:
<?php
session_start();
mysql_connect("xxxxxxxxx", "username", "password") or die(mysql_error());
mysql_select_db("pandajewel1") or die(mysql_error());
$result = mysql_query("SELECT * FROM products WHERE productID > 0") or die(mysql_error());
while($row = mysql_fetch_array( $result ))
{
$productID = $row['productID'];
echo "<form method='post' action='cart.php'>";
echo "<input type='hidden' name='productID' value='$productID'>";
echo "ProductID :$productID | ";
echo "Quantity: <input type='text' name='quantity' value ='1' size='5'> ";
echo "<input type='submit' name='submit' value='ADD'>";
echo "</form><P>";
}
?>
and heres the cart page (name it cart.php) which displays the cart.
<?php
session_start();
//Go into this if statment IF the submit button with a value of add has been pressed
if (isset($_POST['submit']) && $_POST['submit'] == 'ADD')
{//Assign the post values to these vars
$id = $_POST['productID'];
$quantity = $_POST['quantity'];
//Go into this if statment IF the product already exits
if (isset($_SESSION['cart']['productID'][$id]))
{//ADD the $quantity to the the current value
$_SESSION['cart']['quantity'][$id] = $_SESSION['cart']['quantity'][$id] + $quantity;
}
else
{//Add new product into the array
$_SESSION['cart']['productID'][$id] = $id;
$_SESSION['cart']['quantity'][$id] = $quantity;
}
}
$total = count($_SESSION['cart']['productID']);
echo "The are $total different products in your cart.<p>";
echo "<table><tr><td>";
foreach ($_SESSION['cart']['productID'] as $key => $value)
{
echo "ProductID: $value<br>";
}
echo "</td>";
echo "<td>";
foreach ($_SESSION['cart']['quantity'] as $key => $value)
{
echo " Quantity: $value<br>";
}
echo "</td></tr></table>";
?>
Its slightly different from the other example as this one has a seperate variable for quantity and product. You can add as many as you want following the same flow.
Hope this helps you understand!