Hey all, just joined the site because I'm having some serious issues with my final project for my PHP programming class. We were given a basic shopping cart to work off of for a bookstore project, and I just can't figure out how to work the thing. I echo'd the $bookArray array and the result was "Array."
Here's the code:
Code:
<?php
//Shopping cart uses cookies to store cart items.
//PHP script uses an array for adding, removing and displaying the cart items.
//Cookies can contain only string data so array must be serialized.
$cookieName = "myCart2";
// retrieve cookie and unserialize into $bookArray
if (isset($_COOKIE[$cookieName])) {
$bookArray = unserialize($_COOKIE[$cookieName]);
}
// Add items to cart
$addISBN = CleanISBN($_GET['addISBN']);
if (strlen($addISBN) > 0) {
if (isset($addISBN, $bookArray)) {
// Increment by +1
$bookArray[$addISBN] += 1;
} else {
// Add new item to cart
$bookArray[$addISBN] = 1;
}
}
// Remove items from cart
$deleteISBN = CleanISBN($_GET['deleteISBN']);
if (strlen($deleteISBN) > 0) {
if (isset($bookArray[$deleteISBN])) {
// Deincrement by 1
$bookArray[$deleteISBN] -= 1;
// remove ISBN from array if qty==0
if ($bookArray[$deleteISBN] == 0) {
unset($bookArray[$deleteISBN]);
}
}
}
if (isset($bookArray)) {
// Write cookie
setcookie($cookieName, serialize($bookArray), time() + 60 * 60 * 24 * 180);
//Count total books in cart
$totalbooks = 0;
foreach($bookArray as $isbn => $qty) {
$totalbooks += $qty;
}
setCookie('BookCount', $totalbooks, time() + 60 * 60 * 24 * 180);
}
//***************************************************
//You do not need to modify any code above this point
//***************************************************
?>
<html>
<head>
<title>Shopping Cart -- NerdWords.com</title>
</head>
<body>
<div align="center">
<center>
<p>
<? echo $totalbooks . " item";
if ($totalbooks != 1) {
echo 's';
}
if ($totalbooks = 0) {
echo 's';
}
echo ' in your cart'
?>
</p>
<table border="0" cellpadding="5" width="300">
<?
if (count($bookArray)) {
echo "<tr><td>ISBN</td><td>Title</td><td>Qty</td><td>Price</td><td>Add/Remove</td></tr>";
$count = 1;
foreach($bookArray as $isbn => $qty) {
$strSQL = "SELECT ISBN, title, price FROM bookdescriptions WHERE ISBN = '$isbn'";
}
?>
<tr>
<td><a href="ProductPage.php?isbn=<? echo $isbn; ?>"></td>
<td><? echo $row['title']; ?></a></td>
<td><? echo $count ?></td>
<td><? echo $row['price']; ?></td>
<td>
<a href="?addISBN=<? echo $isbn; ?>">Add</a><br>
<a href="?deleteISBN=<? echo $isbn; ?>">Remove</a>
</td>
</tr>
<?
$count += 1;
}
?>
</table>
</center>
</div>
<?php
// ********************************************
// Utilities **********************************
function CleanISBN($isbn)
{
return preg_replace("/[^0-9X]{1,}/", "" , substr($isbn, 0, 10));
}
?>
|