Hi all,
I'm currently working on a shopping cart script:
shoppingbasket.php
<?PHP
ob_start();
include ('functions.php');
session_start();
$cart = $_SESSION["cart"];
$connection=mysql_connect("localhost","","") or die('Could not connect to the database server');
$db = mysql_select_db("shopcart", $connection) or die ("Unable to select database.");
$i=0;
if(!session_is_registered("cart") OR count($cart)==0)
{
print("<center>Your shopping cart is empty=</center><BR>");
print("<center>Return to they= <a href='bookslist.php'>list of books=</a>.</center>");
exit;
}elseif(session_is_registered("cart") AND count($cart)!=0)
{
print("<form action='updatecart.php' method='post'>");
print("<table width='100%' border='0'>");
print("<tr><td align='right'></td></tr></table>");
print("<table width='100%' border='0'>");
print("<tr >
<td height='20' bgcolor='#99CCFF'><b>Shopping Cart Items</b></td>
<td bgcolor='#99CCFF'><b>Quantity</b></td>
<td></td></tr>");
foreach ($cart as $isbn => $qty)
{
$total_price=calculate_price($cart);
$items=calculate_items($cart);
$sql= "SELECT
book_name,
book_author,
book_type,
book_list_price,
book_our_price,
book_rating,
book_id,
book_isbn
FROM
books
WHERE
book_isbn='$isbn'";
$sql_result = mysql_query($sql,$connection) or die ("Could not select data");
while ($row = mysql_fetch_row($sql_result))
{
$book_name= $row[0];
$book_author= $row[1];
$book_type= $row[2];
$book_list_price= $row[3];
$book_our_price= $row[4];
$book_rating= $row[5];
$book_id= $row[6];
$book_isbn= $row[7];
}
print("<tr valign='top'>");
print("<td width='64%'>");
print("<b><a href='books.php?book_isbn=$book_isbn'>$book_name</a></b><br>");
print("by $book_author ($book_type)<br>");
print("Usually ships in 24 hours<br>");
print("</td>");
print("<td width='24%'><input type='text' name='quantity[$i]' size='8' value='$qty'> <b>Our Price: $$book_our_price</b></td>");
print("<td width='7%'><a href='updatecart.php?action=delete&book_isbn=$book_isbn'>Delete</a></td>");
print("</tr>");
print("<tr><td> </td><td> </td></tr>");
print("<tr><td> </td><td> </td></tr>");
$i += 1;
}
print("<table width='100%' border='0'>");
print("<td width='5%' align='right'></td>");
print("<td width='64%' align='right'>If you changed any quantities, please press this button to </td>");
print("<td width='5%'><input type='submit' name='Submit' value='Update'></td>");
print("<td width='19%' align='left'> <b>Subtotal: $$total_price</b></td>");
print("<td width='7%'></td>");
print("</tr>");
print("</table>");
print("</table>");
print("</form>");
}
?>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<p> </p>
<p>Return to they= <a href="bookslist.php">list of books=</a>.</p>
</body>
</html>
<?
ob_end_flush();
?>
At the moment i am able to successfully add one item to the cart.
What i am having trouble with is updating the quantity or deleting that item when either is executed.
Can anyone tell me where i might be going wrong?
Here is the code for updating/deleting etc.
updatecart.php
<?PHP
ob_start();
session_start();
$cart = $_SESSION["cart"];
include ('functions.php');
if(!$action=="delete")
{
$i=0;
foreach ($cart as $isbn => $qty)
{
if ($quantity[$i]!=0)
$cart[$isbn]=$quantity[$i];
else
unset($cart[$isbn]);
$i += 1;
}
}elseif ($action=="delete")
{
foreach ($cart as $isbn => $qty)
{
if($isbn==$book_isbn)
unset($cart[$isbn]);
}
}
header ("Location: shoppingbasket.php");
?>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<p> </p>
<p> </p>
</body>
</html>
<?
ob_end_flush();
?>
functions.php
<?PHP
function calculate_price($cart)
{
$price=0.0;
if(is_array($cart))
{
$connection=mysql_connect("localhost","","") or die('Could not connect to the database server');
$db = mysql_select_db("shopcart", $connection) or die ("Unable to select database.");
foreach($cart as $isbn => $qty)
{
$sql= "SELECT
book_our_price
FROM
books
WHERE
book_isbn='$isbn'";
$sql_result = mysql_query($sql,$connection) or die ("Could not select data");
while ($row = mysql_fetch_row($sql_result))
{
$book_our_price= $row[0];
}
$price += $book_our_price*$qty;
}
}
return $price;
}
function calculate_items($cart)
{
$items=0;
if(is_array($cart))
{
foreach($cart as $book_isbn => $qty)
{
$items += $qty;
}
}
return $items;
}
?>
Regards,
chrima