Hi, I'm new to the forum,
I have had the exact same issue as Vikki had, with every same EXACT problem throughout her posts.
I've taken the code from ShoppingCart.php in Post #23 by Job0107 and replaced it in my Shoppingcart.php. The shopping cart now works perfeclty. You can add items from any category, at any time, and the shopping cart ADDs them perfectly!
Thanks so much for the information you provided job0107!
I'm new to PHP programming too, and also have taken this challenge on my own, as I'm not part of any class, and don't get a grade!
It's too bad the author of our text book (Gosselin) did not review his code before printing his book. There are several errors.
In any case, if anyone needs the entire code from the various pages. Here they are. They are the pages that one creates for you Chapter Directory for Chapter 11 for the book called "PHP Programming with MySQL" by Don Gosselin.
Good luck to all the new students in the future!
ShoppingCart.php
PHP Code:
<?php
class ShoppingCart {
private $DBConnect = "";
private $DBName = "";
private $TableName = "";
private $ItemCount = 0;
public $Orders = array();
public $count = 0;
public function __construct() {
$this->DBConnect = @new mysqli("localhost", "root", "yourownpassword");
if(mysqli_connect_errno()) {
die("<p>Unable to connect to the database server.</p><p>Error code ".mysqli_connect_errno().": ".mysqli_connect_error())."</p>";
}
}
public function __destruct() {
$this->DBConnect->close();
}
public function setDatabase($Database) {
$this->DBName = $Database;
@$this->DBConnect->select_db($this->DBName) or die("<p>Unable to select the database.</p><p>Error code ".mysqli_errno($this->DBConnect).": ".mysqli_error($this->DBConnect))."</p>";
}
public function setTable($Table) {
$this->TableName = $Table;
}
public function getProductList() {
$SQLstring = "SELECT * FROM $this->TableName";
$QueryResult = $this->DBConnect->query($SQLstring) or die("<p>Unable to perform the query.</p><p>Error code ".mysqli_errno($this->DBConnect).": ".mysqli_error($DBConnect))."</p>";
echo "<table width='100%' border='1'><tr><th>Product</th><th>Description</th><th>Price Each</th><th>Select Item</th></tr>";
$Row = $QueryResult->fetch_row();
while($Row) {
echo "<tr><td style='padding-left:5px;'>{$Row[1]}</td><td style='padding-left:5px;'>{$Row[2]}</td>";
printf("<td align='center'>$%.2f</td>", $Row[3]);
echo "<td align='center'><a href='ShowCart.php?PHPSESSID=".session_id()."&operation=addItem&productID=".$Row[0]."'>Add</a></td></tr>";
$Row = $QueryResult->fetch_row();
}
echo "</table>";
}
public function addItem() {
$ProdID = $_GET["productID"];
for($i=0;$i<count($this->Orders);$i++) {
if($this->Orders[$i][0]==$ProdID) {
exit("<div style='width:385px;padding:5px;border:2px solid #ff0000;color:#cc0000;font-zixe:20px;font-weight:bold;'>You already selected that item! Please choose a category.</div>");
}
}
$this->Orders[$this->ItemCount][0] = $ProdID;
$this->Orders[$this->ItemCount][1] = $this->TableName;
$this->ItemCount++;
}
public function __wakeup() {
$this->DBConnect = @new mysqli("localhost", "root", "yourownpassword");
if(mysqli_connect_errno()) {
die("<p>Unable to connect to the database server.</p><p>Error code ".mysqli_connect_errno().": ".mysqli_connect_error())."</p>";
}
@$this->DBConnect->select_db($this->DBName) or die("<p>Unable to select the database.</p><p>Error code ".mysqli_errno($this->DBConnect).": ".mysqli_error($this->DBConnect))."</p>";
}
public function showCart() {
$sw = 0;
for($i=0;$i<count($this->Orders);$i++) {
$Order = $this->Orders[$i];
$Table_Name = $Order[1] ? $Order[1] : "";
$ProdID = $Order[0] ? $Order[0] : "";
$OrderQTY = 1;
if($Table_Name && $ProdID) {
if($sw == 0) {
echo "<table width='100%' border='1'><tr><th>Remove Item</th><th>Product</th><th>Quantity</th><th>Price Each</th></tr>";
$sw = 1;
}
$SQLstring = "SELECT * FROM ".$Table_Name." WHERE productID='".$ProdID."'";
$QueryResult = @mysqli_query($this->DBConnect, $SQLstring) or die("<p>Unable to perform the query.</p><p>Error code ".mysqli_errno($this->DBConnect).": ".mysqli_error($this->DBConnect))."</p>";
$Row = mysqli_fetch_row($QueryResult);
$Total += $Row[3] * $OrderQTY;
$Total_total += $Total;
$Total = 0;
echo "<tr>
<td align='center'><a href='ShowCart.php?PHPSESSID=".session_id()."&operation=removeItem&productID=".$Row[0]."'>Remove</a></td>
<td style='padding-left:10px;'>{$Row[1]}</td>
<td align='center'>$OrderQTY</td>";
printf("<td align='center'>$%.2f</td></tr>", $Row[3]);
$found = TRUE;
$this->count++;
}
}
if(!$found){echo "<table width='100%' border='1'><tr>";}
else {
$link = $Total_total ? "<a href='ShowCart.php?PHPSESSID=".session_id()."&operation=emptyCart'><strong>Empty Cart</strong></a>" : "";
echo "<tr><td align='center'>$link</td>";
}
echo "<td colspan='2'><strong> Your shopping cart contains ".$this->count." product(s).</strong></td>";
printf("<td align='center'><strong>Total: $%.2f</strong></td></tr>", $Total_total);
echo "</table>";
$this->count = 0;
}
public function removeItem() {
$ProdID = $_GET['productID'];
for($i=0;$i<count($this->Orders);$i++) {
if($this->Orders[$i][0] == $ProdID) {
$this->Orders[$i][0] = "";
$this->Orders[$i][1] = "";
}
}
}
public function emptyCart() {
for($i=0;$i<count($this->Orders);$i++) {
$this->Orders[$i][0] = "";
$this->Orders[$i][1] = "";
}
}
}
?>
ShowCart.php
PHP Code:
<?php
session_start();
require_once("ShoppingCart.php");
if (!isset($_SESSION['curCart']))
header("location:GosselinGourmetGoods.php");
?>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Gosselin Gourmet Goods</title>
<link rel="stylesheet" href="php_styles.css" type="text/css" />
</head>
<body>
<h1>Gosselin Gourmet Goods</h1>
<h2>Shop by Category</h2>
<p><a href="GosselinGourmetCoffees.php">Gourmet Coffees</a><br />
<a href="GosselinGourmetOlives.php">Specialty Olives</a><br />
<a href="GosselinGourmetSpices.php">Gourmet Spices</a></p>
<?php
$Cart = unserialize($_SESSION['curCart']);
if (isset($_GET['operation'])) {
if ($_GET['operation'] == "addItem")
$Cart->addItem();
if ($_GET['operation'] == "removeItem")
$Cart->removeItem();
if ($_GET['operation'] == "emptyCart")
$Cart->emptyCart();
}
$Cart->showCart();
$_SESSION['curCart'] = serialize($Cart);
?>
</body>
</html>
GosselingGourmetCoffee.php
PHP Code:
<?php
session_start();
require_once("ShoppingCart.php");
?>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Gosselin Gourmet Goods</title>
<link rel="stylesheet" href="php_styles.css" type="text/css" />
</head>
<body>
<h1>Gosselin Gourmet Goods</h1>
<h2>Shop by Category</h2>
<p><a href="GosselinGourmetCoffees.php">Gourmet Coffees</a><br />
<a href="GosselinGourmetOlives.php">Specialty Olives</a><br />
<a href="GosselinGourmetSpices.php">Gourmet Spices</a></p>
<h2>Gourmet Coffees</h2>
<?php
$Database = "gosselin_gourmet";
$Table = "coffee";
if (isset($_SESSION['curCart']))
$Cart = unserialize($_SESSION['curCart']);
else {
if (class_exists("ShoppingCart")) {
$Cart = new ShoppingCart();
$Cart->setDatabase($Database);
}
else
exit("<p>The ShoppingCart class is not available!</p>");
}
$Cart->setTable($Table);
$Cart->getProductList();
$_SESSION['curCart'] = serialize($Cart);
?>
<p><a href='<?php echo "ShowCart.php?PHPSESSID=" . session_id() ?>'>Show Shopping Cart</a></p>
</body>
</html>
thanks so much once again
Jaime Nacach
