Took this bit of code from a posting on codewalkers:
<?
function addItem($id, $product, $description, $price, $category, $table, $theQuantity=1)
{
$idArray = Array();
$productArray = Array();
$descriptionArray = Array();
$quantityArray = Array();
$priceArray = Array();
$tableArray = Array();
$currentItem;
if(!$_SESSION["idArray"])
{
array_push($idArray, $id);
array_push($productArray, $product);
array_push($descriptionArray, $description);
array_push($quantityArray, $theQuantity);
array_push($priceArray, $price);
array_push($tableArray, $table);
$_SESSION["idArray"] = $idArray;
$_SESSION["productArray"] = $productArray;
$_SESSION["descriptionArray"] = $descriptionArray;
$_SESSION["quantityArray"] = $quantityArray;
$_SESSION["priceArray"] = $priceArray;
$_SESSION["tableArray"] = $tableArray;
}
else
{
for($i = 0; $i<= sizeOf($_SESSION["idArray"]); $i++)
{
if($_SESSION["idArray"][$i] == $id)
$currentItem = $i;
}
if($_SESSION["idArray"][$currentItem] == $id)//check if id exists; if so, increase quantity
{
connectToDB();
//check that quantity in cart not more than quantity in DB
//therefore, retreive quantity from DB & when adding an item, check that it is not more
$query = mysql_query("SELECT quantity FROM $table WHERE title_id = '$id'");
$row = mysql_fetch_array($query);
$quantity = $row[quantity];
mysql_close();
if($_SESSION["quantityArray"][$currentItem] < $quantity)
$_SESSION["quantityArray"][$currentItem] += $theQuantity;
else
{
?>
<script language="javascript1.2" type="text/javascript">
<!--
alert("You have already added the available quantity of this title.\nThere are no more copies in stock.\n");
//-->
</script>
<?
}
}
else//add the item if it's id doesn't exist
{
array_push($_SESSION["idArray"], $id);
array_push($_SESSION["productArray"], $product);
array_push($_SESSION["descriptionArray"], $description);
array_push($_SESSION["quantityArray"], $theQuantity);
array_push($_SESSION["priceArray"], $price);
array_push($_SESSION["tableArray"], $table);
}
}
}