CASHOUT Posted August 9, 2011 Share Posted August 9, 2011 what am i missing here??????? Notice: Undefined index: item_id in /home6/modeljew/public_html/cart.php on line 58 Notice: Undefined index: item_id in /home6/modeljew/public_html/cart.php on line 65 Notice: Undefined variable: product_name in /home6/modeljew/public_html/cart.php on line 67 Notice: Undefined variable: price in /home6/modeljew/public_html/cart.php on line 68 cart.php <?php session_start(); // Start session first thing in script // Script Error Reporting error_reporting(E_ALL); ini_set('display_errors', '1'); // Connect to the MySQL database include "storescripts/connect_to_mysql.php"; ?> <?php //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Section 1 (if user attempts to add something to the cart from the product page) //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// if (isset($_POST['pid'])){ $pid = $_POST['pid']; $wasFound = false; $i = 0; //if the cart session variable is not set or cart arraty is empty if (!isset($_SESSION["cart_array"])||count($_SESSION["cart_array"]) < 1){ //RUN IF THE CART IS EMPTY OR NOT SET $_SESSION["cart_array"] = array(1 => array("item id" => $pid, "quantity" => 1)); } else { // RUN IF THE CART HAS AT LEAST ONE ITEM IN IT foreach($_SESSION["cart_array"] as $each_item){ $i++; while (list($key, $value) = each($each_item)){ if ($key == "item_id" && $value == $pid){ // That item is in cart already so lets adjust its quantity using array_splice() array_splice($_SESSION["cart_array"], $i-1, 1, array(array("item_id" => $pid, "quantity" => $each_item['quantity'] + 1))); $wasFound = true; }// close if condition }// close while loop }// close foreach loop if ($wasFound == false){ array_push($_SESSION["cart_array"], array("item_id" => $pid, "quantity" => 1)); } } } ?> <?php //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Section 2 (if user chooses to empty their shopping cart) //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// if (isset($_GET['cmd']) && $_GET['cmd'] == "emptycart"){ unset($_SESSION["cart_array"]); } ?> <?php //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Section 3 (render the cart for the user to view on the page) //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// $cartOutput = ""; if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1){ $cartOutput = "<h2 align='center'>Your shopping cart is empty</h2>"; } else { $i = 0; foreach ($_SESSION["cart_array"] as $each_item){ $i++; $item_id = $each_item['item_id']; $sql = mysql_query("SELECT * FROM products WHERE id='$item_id' LIMIT 1"); while ($row = mysql_fetch_array($sql)){ $product_name = $row["product_name"]; $price = $row["price"]; } $cartOutput .= "<h2>Cart item $i</h2>"; $cartOutput .= "item ID: " . $each_item['item_id']. "<br />"; $cartOutput .= "Item Quantity: " . $each_item['quantity']. "<br />"; $cartOutput .= "Item Name: " . $product_name . "<br />"; $cartOutput .= "Item Price: " . $price . "<br />"; //while (list($key, $value) = each($each_item)){ //$cartOutput .= "$key: $value<br />"; //} } } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Your Cart</title> <link href="style/style.css" rel="stylesheet" type="text/css" media="screen" /> </head> <body> <div align="center" id="mainWrapper"> <?php include_once("template_header.php");?> <div id="pageContent"> <div style="margin:24px; text-align:left;"> <?php echo $cartOutput; ?> <br /><br /> <a href="cart.php?cmd=emptycart">Click Here to Empty Your Shopping Cart</a> </div> <br /> </div> <?php include_once("template_footer.php");?> </div> </body> </html> Quote Link to comment Share on other sites More sharing options...
WebStyles Posted August 9, 2011 Share Posted August 9, 2011 when you create the cart: //RUN IF THE CART IS EMPTY OR NOT SET $_SESSION["cart_array"] = array(1 => array("item id" => $pid, "quantity" => 1)); you're using 'item id', but then you want to see it, you add an underscore: 'item_id'... that's why it says your variable is not defined. you're using the wrong name. Quote Link to comment Share on other sites More sharing options...
phpSensei Posted August 9, 2011 Share Posted August 9, 2011 Whats the point of while ($row = mysql_fetch_array($sql)){ $product_name = $row["product_name"]; $price = $row["price"]; } inside your loop? Quote Link to comment Share on other sites More sharing options...
CASHOUT Posted August 9, 2011 Author Share Posted August 9, 2011 Webstyles thank you. That fixed the problem. i appreciate you man. //RUN IF THE CART IS EMPTY OR NOT SET $_SESSION["cart_array"] = array(1 => array("item_id" => $pid, "quantity" => 1)); Quote Link to comment Share on other sites More sharing options...
CASHOUT Posted August 9, 2011 Author Share Posted August 9, 2011 PHP Sensei while ($row = mysql_fetch_array($sql)){ $product_name = $row["product_name"]; $price = $row["price"]; } this piece of code is so that, when an item is added to the cart, the product name and price is fetched from the mysql database and displayed on the shopping cart list 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.