aboyce2107 Posted April 20, 2015 Share Posted April 20, 2015 Okay here's the problem, I've got my code typed up and everything seems to be working. However this comes up: Notice: Undefined variable: storeID in \\Chapter 10\ReinforcementExercises\class_OnlineStores.php on line 101 first of all the variable has alread been defined in the class definition. Secondly, I'm having more items being added to the shopping cart when the checkout link is clicked. checkout function public function checkout() { $ProductsOrdered = 0; foreach($this->shoppingCart as $productID => $quantity) { if ($quantity > 0) { ++$ProductsOrdered; $SQLstring = "INSERT INTO orders " . " (orderID, prductID, quantity) " . " VALUES('" . session_id() ."', " . "'$productID', $quantity)"; $QueryResult = $this->DBConnect->query($SQLstring); } } echo "<p><strong>Your order has been " . "recorded.</strong></p>\n"; } and the getProductList function public function getProductList() { $retval = FALSE; $subtotal = 0; if (count($this->inventory) > 0) { echo "<table width='100%'>\n"; echo "<tr><th>Product</th><th>Description</th>" . "<th>Price Each</th><th># in Cart</th>" . "<th>Total Price</th><th> </th></tr>\n"; foreach ($this->inventory as $ID => $Info) { echo "<tr><td>" . htmlentities($Info['name']) . "</td>\n"; echo "<td>" . htmlentities($Info['description']) . "</td>\n"; printf("<td class='currency'>$%.2f</td>\n", $Info['price']); echo "<td class='currency'>" . $this->shoppingCart[$ID] . "</td>\n"; printf("<td class='currency'>$%.2f </td>\n", $Info['price'] * $this->shoppingCart[$ID]); echo "<td><a href='" . $_SERVER['SCRIPT_NAME'] . "?PHPSESSID=" . session_id() . "&ItemToAdd=$ID'>Add " . " Item</a><br />\n"; echo "<a href='" . $_SERVER['SCRIPT_NAME'] . "?PHPSESSID=" . session_id() . "&ItemToRemove=$ID'>Remove " . " Item</a></td>\n"; $subtotal += ($Info['price'] * $this->shoppingCart[$ID]); } echo "<tr><td colspan='4'>Subtotal</td>\n"; printf("<td class='currency'>$%.2f</td>\n", $subtotal); echo "<td><a href='" . $_SERVER['SCRIPT_NAME'] . "?PHPSESSID=" . session_id() . "&EmptyCart=TRUE'>Empty " . " Cart</a></td></tr>\n"; echo "</table>"; echo "<p><a href=' 'Checkout.php?PHPSESSID='" . session_id() . "&CheckOut=$storeID'>Checkout</a></p>\n"; - this is where the error is being indicated. $retval = TRUE; } return($retval); } I'd appreciate any help anyone can provide... Quote Link to comment Share on other sites More sharing options...
IThinkMyBrainHurts Posted April 20, 2015 Share Posted April 20, 2015 If that's all in a class then you'd access the variable using $this->storeID Oh, please edit your post and use code tags to wrap your code!!! Quote Link to comment Share on other sites More sharing options...
aboyce2107 Posted April 22, 2015 Author Share Posted April 22, 2015 Yeah, sorry about that I was in a hurry when I posted this. Here's what the code looks like. <?php class OnlineStore { private $DBConnect = NULL; private $storeID = ""; private $inventory = array(); private $shoppingCart = array(); function __construct() { include("inc_OnlineStoreDB.php"); $this->DBConnect = $DBConnect; } function __destruct() { if (!$this->DBConnect->connect_error) $this->DBConnect->close(); } public function setStoreID($storeID) { if ($this->storeID != $storeID) { $this->storeID = $storeID; $SQLstring = "SELECT * FROM inventory " . " where storeID = '" . $this->storeID . "'"; $QueryResult = @$this->DBConnect->query($SQLstring); if ($QueryResult === FALSE) { $this->storeID = ""; } else { $this->inventory = array(); $this->shoppingCart = array(); while (($Row = $QueryResult->fetch_assoc()) !== NULL) { $this->inventory[$Row['productID']] = array(); $this->inventory[$Row['productID']]['name'] = $Row['name']; $this->inventory[$Row['productID']]['description'] = $Row['description']; $this->inventory[$Row['productID']]['price'] = $Row['price']; $this->shoppingCart[$Row['productID']] = 0; } } } } public function getStoreInformation() { $retval = FALSE; if ($this->storeID != "") { $SQLstring = "SELECT * FROM store_info " . " where storeID = '" . $this->storeID . "'"; $QueryResult = @$this->DBConnect->query($SQLstring); if ($QueryResult !== FALSE) { $retval = $QueryResult->fetch_assoc(); } } return($retval); } public function getProductList() { $retval = FALSE; $subtotal = 0; if (count($this->inventory) > 0) { echo "<table width='100%'>\n"; echo "<tr><th>Product</th><th>Description</th>" . "<th>Price Each</th><th># in Cart</th>" . "<th>Total Price</th><th> </th></tr>\n"; foreach ($this->inventory as $ID => $Info) { echo "<tr><td>" . htmlentities($Info['name']) . "</td>\n"; echo "<td>" . htmlentities($Info['description']) . "</td>\n"; printf("<td class='currency'>$%.2f</td>\n", $Info['price']); echo "<td class='currency'>" . $this->shoppingCart[$ID] . "</td>\n"; printf("<td class='currency'>$%.2f </td>\n", $Info['price'] * $this->shoppingCart[$ID]); echo "<td><a href='" . $_SERVER['SCRIPT_NAME'] . "?PHPSESSID=" . session_id() . "&ItemToAdd=$ID'>Add " . " Item</a><br />\n"; echo "<a href='" . $_SERVER['SCRIPT_NAME'] . "?PHPSESSID=" . session_id() . "&ItemToRemove=$ID'>Remove " . " Item</a></td>\n"; $subtotal += ($Info['price'] * $this->shoppingCart[$ID]); } echo "<tr><td colspan='4'>Subtotal</td>\n"; printf("<td class='currency'>$%.2f</td>\n", $subtotal); echo "<td><a href='" . $_SERVER['SCRIPT_NAME'] . "?PHPSESSID=" . session_id() . "&EmptyCart=TRUE'>Empty " . " Cart</a></td></tr>\n"; echo "</table>"; echo "<p><a href=' 'Checkout.php?PHPSESSID='" . session_id() . "&CheckOut=$storeID'>Checkout</a></p>\n"; - line of code that the error is indicating $retval = TRUE; } return($retval); } public function addItem() { $ProdID = $_GET['ItemToAdd']; if (array_key_exists($ProdID, $this->shoppingCart)) $this->shoppingCart[$ProdID] += 1; } function __wakeup() { include("inc_OnlineStoreDB.php"); $this->DBConnect = $DBConnect; } private function removeItem() { $ProdID = $_GET['ItemToRemove']; if (array_key_exists($ProdID, $this->shoppingCart)) if ($this->shoppingCart[$ProdID]>0) $this->shoppingCart[$ProdID] -= 1; } private function emptyCart() { foreach ($this->shoppingCart as $key => $value) $this->shoppingCart[$key] = 0; } public function processUserInput() { if (!empty($_GET['ItemToAdd'])) $this->addItem(); if (!empty($_GET['ItemToRemove'])) $this->removeItem(); if (!empty($_GET['EmptyCart'])) $this->emptyCart(); } public function checkout() { $ProductsOrdered = 0; foreach($this->shoppingCart as $productID => $quantity) { if ($quantity > 0) { ++$ProductsOrdered; $SQLstring = "INSERT INTO orders " . " (orderID, prductID, quantity) " . " VALUES('" . session_id() ."', " . "'$productID', $quantity)"; $QueryResult = $this->DBConnect->query($SQLstring); } } echo "<p><strong>Your order has been " . "recorded.</strong></p>\n"; } } ?> Quote Link to comment Share on other sites More sharing options...
aboyce2107 Posted April 22, 2015 Author Share Posted April 22, 2015 Okay the error's gone now, but I'm still having the adding of items when the checkout link is clicked!!! I'm just not seeing why it's doing that, I'm sure it's obvious though. Quote Link to comment Share on other sites More sharing options...
joel24 Posted April 22, 2015 Share Posted April 22, 2015 what is the code on checkout.php ? use the code tag when you post PHP - when you're posting it's the little blue icon that looks like this: < > Quote Link to comment Share on other sites More sharing options...
maxxd Posted April 22, 2015 Share Posted April 22, 2015 (edited) Okay the error's gone now It shouldn't be - I still don't see where you're defining $storeID in getProductList(). Unless the code has changed, you should still be getting the undefined variable error. Edited April 22, 2015 by maxxd Quote Link to comment Share on other sites More sharing options...
aboyce2107 Posted April 22, 2015 Author Share Posted April 22, 2015 <?php class OnlineStore { private $DBConnect = NULL; private $storeID = ""; private $inventory = array(); private $shoppingCart = array(); function __construct() { include("inc_OnlineStoreDB.php"); $this->DBConnect = $DBConnect; } function __destruct() { if (!$this->DBConnect->connect_error) $this->DBConnect->close(); } public function setStoreID($storeID) { if ($this->storeID != $storeID) { $this->storeID = $storeID; $SQLstring = "SELECT * FROM inventory " . " where storeID = '" . $this->storeID . "'"; $QueryResult = @$this->DBConnect->query($SQLstring); if ($QueryResult === FALSE) { $this->storeID = ""; } else { $this->inventory = array(); $this->shoppingCart = array(); while (($Row = $QueryResult->fetch_assoc()) !== NULL) { $this->inventory[$Row['productID']] = array(); $this->inventory[$Row['productID']]['name'] = $Row['name']; $this->inventory[$Row['productID']]['description'] = $Row['description']; $this->inventory[$Row['productID']]['price'] = $Row['price']; $this->shoppingCart[$Row['productID']] = 0; } } } } public function getStoreInformation() { $retval = FALSE; if ($this->storeID != "") { $SQLstring = "SELECT * FROM store_info " . " where storeID = '" . $this->storeID . "'"; $QueryResult = @$this->DBConnect->query($SQLstring); if ($QueryResult !== FALSE) { $retval = $QueryResult->fetch_assoc(); } } return($retval); } public function getProductList() { $retval = FALSE; $subtotal = 0; if (count($this->inventory) > 0) { echo "<table width='100%'>\n"; echo "<tr><th>Product</th><th>Description</th>" . "<th>Price Each</th><th># in Cart</th>" . "<th>Total Price</th><th> </th></tr>\n"; foreach ($this->inventory as $ID => $Info) { echo "<tr><td>" . htmlentities($Info['name']) . "</td>\n"; echo "<td>" . htmlentities($Info['description']) . "</td>\n"; printf("<td class='currency'>$%.2f</td>\n", $Info['price']); echo "<td class='currency'>" . $this->shoppingCart[$ID] . "</td>\n"; printf("<td class='currency'>$%.2f </td>\n", $Info['price'] * $this->shoppingCart[$ID]); echo "<td><a href='" . $_SERVER['SCRIPT_NAME'] . "?PHPSESSID=" . session_id() . "&ItemToAdd=$ID'>Add " . " Item</a><br />\n"; echo "<a href='" . $_SERVER['SCRIPT_NAME'] . "?PHPSESSID=" . session_id() . "&ItemToRemove=$ID'>Remove " . " Item</a></td>\n"; $subtotal += ($Info['price'] * $this->shoppingCart[$ID]); } echo "<tr><td colspan='4'>Subtotal</td>\n"; printf("<td class='currency'>$%.2f</td>\n", $subtotal); echo "<td><a href='" . $_SERVER['SCRIPT_NAME'] . "?PHPSESSID=" . session_id() . "&EmptyCart=TRUE'>Empty " . " Cart</a></td></tr>\n"; echo "</table>"; echo "<p><a href=' 'Checkout.php?PHPSESSID='" . session_id() . "&CheckOut=$this->storeID'>Checkout</a></p>\n"; $retval = TRUE; } return($retval); } public function addItem() { $ProdID = $_GET['ItemToAdd']; if (array_key_exists($ProdID, $this->shoppingCart)) $this->shoppingCart[$ProdID] += 1; } function __wakeup() { include("inc_OnlineStoreDB.php"); $this->DBConnect = $DBConnect; } private function removeItem() { $ProdID = $_GET['ItemToRemove']; if (array_key_exists($ProdID, $this->shoppingCart)) if ($this->shoppingCart[$ProdID]>0) $this->shoppingCart[$ProdID] -= 1; } private function emptyCart() { foreach ($this->shoppingCart as $key => $value) $this->shoppingCart[$key] = 0; } public function processUserInput() { if (!empty($_GET['ItemToAdd'])) $this->addItem(); if (!empty($_GET['ItemToRemove'])) $this->removeItem(); if (!empty($_GET['EmptyCart'])) $this->emptyCart(); } public function checkout() { $ProductsOrdered = 0; foreach($this->shoppingCart as $productID => $quantity) { if ($quantity > 0) { ++$ProductsOrdered; $SQLstring = "INSERT INTO orders " . " (orderID, prductID, quantity) " . " VALUES('" . session_id() ."', " . "'$productID', $quantity)"; $QueryResult = $this->DBConnect->query($SQLstring); } } echo "<p><strong>Your order has been " . "recorded.</strong></p>\n"; } } ?> Sorry about that I'm still getting used to this site!!! Anyways this is the corrected code! Quote Link to comment Share on other sites More sharing options...
aboyce2107 Posted April 22, 2015 Author Share Posted April 22, 2015 what is the code on checkout.php ? use the code tag when you post PHP - when you're posting it's the little blue icon that looks like this: < > Thanks for the help about posting php code. Here's the checkout.php <?php session_start(); require_once("class_OnlineStores.php"); $storeID = $_GET['CheckOut']; $storeInfo = array(); if (class_exists("OnlineStore")) { if (isset($_SESSION['currentStore'])) $Store = unserialize($_SESSION['currentStore']); else{ $Store = new OnlineStore(); } $Store->setStoreID($storeID); $storeInfo = $Store->getStoreInformation(); } else { $ErrorMsgs[] = "The OnlineStore class is not available !"; $Store = NULL; } ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title><?php echo $storeInfo['name']; ?> Checkout</title> <link rel="stylesheet" type="text/css" href="<?php echo $storeInfo['css_file']; ?>" /> </head> <body> <h1><?php echo htmlentities($storeInfo['name']); ?></h1> <h2>Checkout</h2> <?php $Store->checkout(); ?> </body> </html> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.