i'm using the following script as part of a mutli user shopping cart application made with php and postgres.
the problem i've been having is in calculating a total cost for all the items in the cart.
i would really appreciate any help people can offer me to make this work. thanks a lot.
In case its not obvious each time a row is added to the basket the sum cost is worked out for that row using:
$sumCost = $quantity*$price;
what i want is to be able to calculate is the total sum of every sum cost from every row in the basket, regardless of how many rows there are.
<?php
session_start();
?>
<?php
$DBconnection = pg_connect("");
if (!$DBconnection)
{
print"<h3>Error connecting to the database!</h3>";
}
else
{
$productid = $HTTP_POST_VARS["productid"];
$quantity = $HTTP_POST_VARS["quantity"];
//$tempsession = session_id();
//search basket1 for previous additions
$searchbasket = "select productid from basket where productid like '%$productid' AND tempsession like '%$PHPSESSID'";
$dosearchbasket = pg_exec($DBconnection, $searchbasket);
//count number of rows returned from search result
$rowCounting = pg_numrows($dosearchbasket);
//if previous items exist, then update
if ($rowCounting>=1)
{
//**UPDATING
$Updatebasket = "update basket set quantity=$quantity where productid=$productid AND tempsession='$PHPSESSID'";
$doUpdateBasket = pg_exec($DBconnection, $Updatebasket);
if (!$doUpdateBasket)
{
print "<h2>Error adding it to the basket</h2>";
}
}
//if previous items do not exist, then insert
if ($rowCounting==0)
{
//**INSERTING
$addtobasket = "insert into basket values ($productid, $quantity, '$PHPSESSID')";
$doAddToBasket = pg_exec($DBconnection, $addtobasket);
if (!$doAddToBasket)
{
print "<h2>Error adding to the basket</h2>";
};
}
$selectBasketContents = "select basket.productid, basket.quantity, products.name, products.brand, products.price from basket, products where basket.productid=products.id and basket.tempsession='$PHPSESSID'";
$getBasketContents = pg_exec($DBconnection, $selectBasketContents);
if(!$getBasketContents)
{
print "<h2>Failed selecting basket contents</h2>";
}
else
{
$rowCount = pg_numrows($getBasketContents);
print "<center><table width=75% bgcolor=#C0D9D9 border=1>\n <th class=\"title\" colspan=\"8\">Basket Contents</th>";
print"<tr width=75% bgcolor=#A8A8A8><td class=\"title\">Product ID</td><td class=\"title\">Name</td><td class=\"title\"> Brand</td><td class=\"title\">Price</td><td class=\"title\">Quantity</td><td class=\"title\">Total Price</td><td class=\"title\">Update</td><td class=\"title\">Delete</td></tr>\n";
for($i; $i<$rowCount; $i++)
{
list($productid, $quantity, $name, $brand, $price) = pg_fetch_row($getBasketContents, $i);
$sumCost = $quantity*$price;
print "<tr><FORM ACTION=\"updatebasket.php\" METHOD=\"POST\"><td>$productid</td><input type=\"hidden\" name=\"productid\" value=\"$productid\"><td>$name</td><td>$brand</td><td>£$price</td><td><input type =\"TEXT\" name=\"quantity\" Size=\"4\" MAXLENGTH=\"5\" value=\"$quantity\"></td><td>£$sumCost</td><td><INPUT TYPE=\"submit\" VALUE=\"Update\"></td></FORM>";
print "<td><FORM ACTION=\"alterbasket.php\" METHOD=\"POST\"><input type=\"hidden\" name=\"productid\" value=\"$productid\"><input type=\"hidden\" name=\"quantity\" value=\"$quantity\"><INPUT TYPE=\"submit\" VALUE=\"Delete\"></td></FORM></tr>";
print"</table></center>";
}
}
pg_close();
?>
Thanks again