i've created a shopping cart (using php 4.06 and postgres) that displays items selected by a user in a table. i've been able to work out a cost for multiple quantities of one item ($sumCost) but not for the cost of all the items together. i'm quite new to php and would appreciate any help people could give me to make this work, thankyou.
This is the script that adds items to the basket:
<?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"];
$addtobasket = "insert into basket values ($productid, $quantity)";
$doAddToBasket = pg_exec($DBconnection, $addtobasket);
if (!$doAddToBasket)
{
print "<h2>Error adding to the basket</h2>";
}
$selectBasketContents = "select basket.productid, basket.quantity, jeans.name, jeans.brand, jeans.price from basket, jeans where basket.productid=jeans.id";
$getBasketContents = pg_exec($DBconnection, $selectBasketContents);
if(!$getBasketContents)
{
print "<h2>Failed selecting basket contents</h2>";
}
else
{
$rowCount = pg_numrows($getBasketContents);
print "<table>\n <th class=\"title\" colspan=\"7\">Basket Contents</th>";
print"<tr><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\">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>";
}
}
pg_close();
?>
Thanks for taking the time to look through my code.