I am having problems with a viewcart script that i am working on at the moment. The items are added to a session using cartadd.php:
<?php
session_name ('myStore');
session_start();
if (is_numeric ($_GET['id'])) {
$id = $_GET['id'];
$page_title = 'Add to Cart';
include_once ('includes/header.inc');
include_once ('includes/nav.inc');
if (isset ($_SESSION['cart'][$id])) {
$qty = $_SESSION['cart'][$id] + 1;
} else {
$qty = 1;
}
$_SESSION['cart'][$id] = $qty;
echo '<p>The item has been added to your shopping cart.</p>';
include_once ('includes/footer.inc');
} else {
header ("Location: http://" . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . "/index.php");
exit();
}
?>
This seems to be working okay now, but the viewcart script that I am working on is playing up and I can't seem to work out why.
<?php
session_name ('myStore');
session_start();
$page_title = 'View Your Shopping Cart';
include_once ('includes/header.inc');
if (isset ($_POST['submit'])) {
foreach ($_POST['qty'] as $key => $value) {
if ( ($value == 0) AND (is_numeric ($value)) ) {
unset ($_SESSION['cart'][$key]);
} elseif ( is_numeric ($value) AND ($value > 0) ) {
$_SESSION['cart'][$key] = $value;
}
}
}
$empty = TRUE;
if (isset ($_SESSION['cart'])) {
foreach ($_SESSION['cart'] as $key => $value) {
if (isset($value)) {
$empty = FALSE;
}
}
}
if (!$empty) {
require_once ('mysqldbc.php');
$query = 'SELECT * FROM fps_items IN (';
foreach ($_SESSION['cart'] as $key => $value) {
$query .= $key . ',';
}
$query = substr ($query, 0, -1) . ') ORDER BY item_name ASC';
$result = mysql_query ($query);
echo '<table border="0" width="90%" cellspacing="3" cellpadding="3" align="center">
<tr>
<td align="left" width="30%"><b>id</b></td>
<td align="left" width="30%"><b>Name</b></td>
<td align="right" width="10%"><b>Price</b></td>
<td align="center" width="10%"><b>Qty</b></td>
<td align="right" width="10%"><b>Total Price</b></td>
</tr>
<form action="view_cart.php" method="post"> ';
$total = 0;
while ($row = mysql_fetch_array ($result, MYSQL_ASSOC)) {
$subtotal = $_SESSION['cart'][$row['item_id']] * $row['item_price'];
$total += $subtotal;
echo " <tr>
<td align=\"left\">{$row['item_id']}</td>
<td align=\"left\">{$row['item_name']}</td>
<td align=\"right\">\${$row['item_price']}</td>
<td align=\"center\"><input type=\"text\" size=\"3\" name=\"qty[{$row['item_id']}]\" value=\"{$_SESSION['cart'][$row['item_id']]}\" /></td>
<td align=\"right\">$" . number_format ($subtotal, 2) . "</td>
</tr>\n";
}
echo ' <tr>
<td colspan="4" align="right"><b>Total:<b></td>
<td align="right">$' . number_format ($total, 2) . '</td>
</tr>
</table><div align="center"><input type="submit" name="submit" value="Update My Cart" /></form><br /><br /><a href="checkout.php"><font size="+3">Checkout</font></a></div>';
mysql_close();
} else {
echo '<p>Your cart is currently empty.</p>';
}
include_once ('includes/footer.inc');
?>
I think that it may have something to do with the SQL query. Basically I have a table called fps_items which contains all of the data to do with the item; price, name description ... but I want to get and print only those that have been added to the basket.
Any suggestions please?
Jamie 🙂