
CASHOUT
Members-
Posts
27 -
Joined
-
Last visited
About CASHOUT
- Birthday December 9
Profile Information
-
Gender
Male
CASHOUT's Achievements

Newbie (1/5)
0
Reputation
-
finished product before, scripting variables for website payment standard, and adding PayPal checkout to the 3rd- party shopping cart. <?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(0 => 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)); } } header("location: cart.php"); exit(); } ?> <?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 (if user chooses to adjust item quantuty) //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// if (isset($_POST['item_to_adjust']) && $_POST['item_to_adjust'] != ""){ // execute some code $item_to_adjust = $_POST['item_to_adjust']; $quantity = $_POST['quantity']; $quantity = preg_replace('#[^0-9]#i', '', $quantity); // filter everything but numbers if ($quantity >=2){ $quantity = 1;} if ($quantity < 1){ $quantity = 1;} if ($quantity == ""){ $quantity = 1;} $i = 0; foreach($_SESSION["cart_array"] as $each_item){ $i++; while (list($key, $value) = each($each_item)){ if ($key == "item_id" && $value == $item_to_adjust){ // 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" => $item_to_adjust, "quantity" => $quantity))); }// close if condition }// close while loop }// close foreach loop header("location: cart.php"); exit(); } ?> <?php //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Section 4 (if user wants to remove an item from the cart) //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// if (isset($_POST['index_to_remove']) && $_POST['index_to_remove'] !=""){ // Access the array and run code to remove that array index $key_to_remove = $_POST['index_to_remove']; if (count($_SESSION["cart_array"]) <= 1){ unset($_SESSION["cart_array"]); } else { unset($_SESSION["cart_array"]["$key_to_remove"]); sort($_SESSION["cart_array"]); } header("location: cart.php"); exit(); } ?> <?php //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Section 5 (render the cart for the user to view on the page) //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// $cartOutput = ""; $cartTotal = ""; 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){ $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"]; $details = $row["details"]; } $pricetotal = $price * $each_item['quantity']; //this line calculates final price for duplicate items in cart $cartTotal = $pricetotal + $cartTotal; setlocale(LC_MONETARY , "en_US"); $pricetotal = money_format("%10.2n", $pricetotal); // Dynamic table row assembly $cartOutput .= "<tr>"; $cartOutput .= '<td><a href="product.php?id=' . $item_id . '">' . $product_name . '</a><br/><img src="inventory_images/' . $item_id . '.jpg"alt="' . $product_name . '" width="40" height="52" border="1" /></td>'; $cartOutput .= '<td>' . $details . '</td>'; $cartOutput .= '<td>$' . $price . '</td>'; $cartOutput .= '<td><form action="cart.php" method="post"> <input name="quantity" type="text" value="' . $each_item['quantity']. '" size="1" maxlength="1" /> <input name="adjustBtn' . $item_id . '" type="submit" value="change" /> <input name="item_to_adjust" type="hidden" value="' . $item_id . '" /> </form></td>'; //$cartOutput .= "<td>' . $each_item['quantity']. '</td>"; $cartOutput .= '<td>' . $pricetotal . '</td>'; $cartOutput .= '<td><form action="cart.php" method="post"><input name="deleteBtn' . $item_id . '" type="submit" value="X" /><input name="index_to_remove" type="hidden" value="' . $i . '" /></form></td>'; $cartOutput .= '</tr>'; $i++; } setlocale(LC_MONETARY , "en_US"); $cartTotal = money_format("%10.2n", $cartTotal); $cartTotal = "<div style='font-size: 18px; margin-top: 12px;' align='right'>Cart Total : $".$cartTotal." USD </div>"; } ?> <!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;"> <br /> <table width="100%" border="1" cellspacing="0" cellpadding="6"> <tr> <td width="18%" bgcolor="#99CCFF"><strong>Product</strong></td> <td width="39%" bgcolor="#99CCFF"><strong>Product Description</strong></td> <td width="12%" bgcolor="#99CCFF"><strong>Unit Price</strong></td> <td width="11%" bgcolor="#99CCFF"><strong>Quantity</strong></td> <td width="10%" bgcolor="#99CCFF"><strong>Total</strong></td> <td width="10%" bgcolor="#99CCFF"><strong>Remove</strong></td> </tr> <?php echo $cartOutput; ?> <!--<tr> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> </tr> --> </table> <?php echo $cartTotal; ?> <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>
-
I played with the code and got it to work. Not a 100% sure what I did tho. Lol I'm in my happy place
-
the page does exist. i had it working at one point. I added more code; changed some double quotes to single quotes. Not sure if that's what caused my issue???
-
the product_image is supposed to redirect you to the product.php page when you click on it. instead im getting a error 404 any ideas?? <?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(0 => 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)); } } header("location: cart.php"); exit(); } ?> <?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 (if user chooses to adjust item quantuty) //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// if (isset($_POST['item_to_adjust']) && $_POST['item_to_adjust'] != ""){ // execute some code $item_to_adjust = $_POST['item_to_adjust']; $quantity = $_POST['quantity']; $i = 0; foreach($_SESSION["cart_array"] as $each_item){ $i++; while (list($key, $value) = each($each_item)){ if ($key == "item_id" && $value == $item_to_adjust){ // 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" => $item_to_adjust, "quantity" => $quantity))); }// close if condition }// close while loop }// close foreach loop header("location: cart.php"); exit(); } ?> <?php //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Section 4 (if user wants to remove an item from the cart) //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// if (isset($_POST['index_to_remove']) && $_POST['index_to_remove'] !=""){ // Access the array and run code to remove that array index $key_to_remove = $_POST['index_to_remove']; if (count($_SESSION["cart_array"]) <= 1){ unset($_SESSION["cart_array"]); } else { unset($_SESSION["cart_array"]["$key_to_remove"]); sort($_SESSION["cart_array"]); } header("location: cart.php"); exit(); } ?> <?php //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Section 5 (render the cart for the user to view on the page) //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// $cartOutput = ""; $cartTotal = ""; 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){ $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"]; $details = $row["details"]; } $pricetotal = $price * $each_item['quantity']; //this line calculates final price for duplicate items in cart $cartTotal = $pricetotal + $cartTotal; setlocale(LC_MONETARY , "en_US"); $pricetotal = money_format("%10.2n", $pricetotal); // Dynamic table row assembly $cartOutput .= "<tr>"; $cartOutput .= '<td><a href=\"product.php?id=' . $item_id . '">' . $product_name . '</a><br/><img src="inventory_images/' . $item_id . '.jpg" alt="' . $product_name . '" width="40" height="52" border="1" /></td>'; $cartOutput .= '<td>' . $details . '</td>'; $cartOutput .= '<td>$' . $price . '</td>'; $cartOutput .= '<td><form action="cart.php" method="post"> <input name="quantity" type="text" value="' . $each_item['quantity']. '" size="1" maxlength="1" /> <input name="adjustBtn' . $item_id . '" type="submit" value="change" /> <input name="item_to_adjust" type="hidden" value="' . $item_id . '" /> </form></td>'; //$cartOutput .= '<td>' . $each_item['quantity']. '</td>'; $cartOutput .= '<td>' . $pricetotal . '</td>'; $cartOutput .= '<td><form action="cart.php" method="post"><input name="deleteBtn' . $item_id . '" type="submit" value="X" /><input name="index_to_remove" type="hidden" value="' . $i . '" /></form></td>'; $cartOutput .= '</tr>'; $i++; } setlocale(LC_MONETARY , "en_US"); $cartTotal = money_format("%10.2n", $cartTotal); $cartTotal = "<div style='font-size: 18px; margin-top: 12px;' align='right'>Cart Total : $".$cartTotal." USD </div>"; } ?> <!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;"> <br /> <table width="100%" border="1" cellspacing="0" cellpadding="6"> <tr> <td width="18%" bgcolor="#99CCFF"><strong>Product</strong></td> <td width="39%" bgcolor="#99CCFF"><strong>Product Description</strong></td> <td width="12%" bgcolor="#99CCFF"><strong>Unit Price</strong></td> <td width="11%" bgcolor="#99CCFF"><strong>Quantity</strong></td> <td width="10%" bgcolor="#99CCFF"><strong>Total</strong></td> <td width="10%" bgcolor="#99CCFF"><strong>Remove</strong></td> </tr> <?php echo $cartOutput; ?> <!--<tr> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> </tr> --> </table> <?php echo $cartTotal; ?> <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>
-
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
-
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));
-
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>
-
Could the problem be in the array_splice syntax?
-
ok so the cart runs, but when i add the same item to the shopping cart, instead of recording the same item id and making it a count of 2, it adds a new item_id.. notice the underscore. please help if you can cart.php <?php session_start(); // Start session first thing in script // Script Error Reporting error_reporting(E_ALL); ini_set('display_errors', '1'); ?> <?php 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) //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// $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++; $cartOutput .= "<h2>Cart item $i</h2>"; 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>
-
thanks MasterACE14. I was missing a curly bracket after the array_push. <?php session_start(); // Start session first thing in script // Script Error Reporting error_reporting(E_ALL); ini_set('display_errors', '1'); ?> <?php 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) //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// $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++; $cartOutput .= "<h2>Cart item $i</h2>"; 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>
-
I just got done scripting a shopping cart. When i try to run the cart I get a syntax error message. I'm sure I'm missing something simple, but i cant see it ERROR MESSAGE: Parse error: syntax error, unexpected $end in /home6/modeljew/public_html/cart.php on line 80 cart.php <?php session_start(); // Start session first thing in script // Script Error Reporting error_reporting(E_ALL); ini_set('display_errors', '1'); ?> <?php 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) //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// $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++; $cartOutput .= "<h2>Cart item $i</h2>"; 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>
-
I see it. I added $product_list to the inventory_edit.php script and it solved the problem. ty
-
Inventory_list.php <?php session_start(); if (!isset($_SESSION["manager"])) { header("location: admin_login.php"); exit(); } // Be sure to check that this manager SESSION value is in fact in the database $managerID = preg_replace('#[^0-9]#i', '', $_SESSION["id"]); // filter everything but numbers and letters $manager = preg_replace('#[^A-Za-z0-9]#i', '', $_SESSION["manager"]); // filter everything but numbers and letters $password = preg_replace('#[^A-Za-z0-9]#i', '', $_SESSION["password"]); // filter everything but numbers and letters // Run mySQL query to be sure that this person is an admin and that their password session var equals the database information // Connect to the MySQL database include "../storescripts/connect_to_mysql.php"; $sql = mysql_query("SELECT * FROM admin WHERE id='$managerID' AND username='$manager' AND password='$password' LIMIT 1"); // query the person // ------- MAKE SURE PERSON EXISTS IN DATABASE --------- $existCount = mysql_num_rows($sql); // count the row nums if ($existCount == 0) { // evaluate the count echo "Your login session data is not on record in the database."; exit(); } ?> <?php // Error Reporting error_reporting(E_ALL); ini_set('display_errors','1'); ?> <?php // Delete item Question to Admin, and delete product if they choose if (isset($_GET['deleteid'])) { echo 'Do you really want to delete product with ID of'.$_GET['deleteid'].'?<a href="inventory_list.php?yesdelete='.$_GET['deleteid'].'">Yes</a>|<a href="inventory_list.php">No</a>'; exit(); } if (isset($_GET['yesdelete'])) { // remove item from system and delete its picture // delete from the database $id_to_delete = $_GET['yesdelete']; $sql = mysql_query("DELETE FROM products WHERE id='$id_to_delete'LIMIT 1") or die(mysql_error()); //unlink the image from server // Remove The Pic -------------------------- $pictodelete = ("../inventory_images/$id_to_delete.jpg"); if (file_exists($pictodelete)) { unlink($pictodelete); } header("location: inventory_list.php"); exit(); } ?> <?php // Parse the form data and add inventory item to the system if (isset($_POST['product_name'])) { $product_name = mysql_real_escape_string($_POST['product_name']); $price = mysql_real_escape_string($_POST['price']); $category = mysql_real_escape_string($_POST['category']); $subcategory = mysql_real_escape_string($_POST['subcategory']); $details = mysql_real_escape_string($_POST['details']); // See if that the product name is an identical match to another product in the system $sql = mysql_query("SELECT id FROM products WHERE product_name='$product_name' LIMIT 1"); $productMatch = mysql_num_rows($sql); // count the output amount if ($productMatch > 0) { echo 'Sorry you tried to place a duplicate "Product Name" into the system, <a herf="inventory_list.php">click here</a>'; exit(); } // Add this product into the database now $sql = mysql_query("INSERT INTO products (product_name, price, details, category, subcategory, date_added) VALUES('$product_name','$price','$details','$category','$subcategory',now())") or die (mysql_error()); $pid = mysql_insert_id(); // Place image in the folder $newname = "$pid.jpg"; move_uploaded_file($_FILES['fileField']['tmp_name'], "../inventory_images/$newname"); header("location: inventory_list.php"); exit(); } ?> <?php // This block grabs the whole list for viewing $product_list = ""; $sql = mysql_query("SELECT * FROM products ORDER BY date_added DESC"); $productCount = mysql_num_rows($sql); //count the output amount if ($productCount > 0) { while($row = mysql_fetch_array($sql)){ $id = $row["id"]; $product_name = $row["product_name"]; $date_added = strftime("%b %d, %Y", strtotime($row["date_added"])); $product_list .="$date_added - $id - $product_name <a href='inventory_edit.php?pid=$id'>edit</a> • <a href='inventory_list.php?deleteid=$id'>delete</a><br />"; } } else { $product_list = "You have no products listed in your store yet"; } ?> <!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>Inventory List</title> <link rel="stylesheet" href="../style/style.css" type="text/css" media="screen" /> </head> <body> <div align="center" id="mainWrapper"> <?php include_once("../template_header.php");?> <div id="pageContent"><br /> <div align="right" style="margin-right:32px;"><a href="inventory_list.php#inventoryForm">+ Add New Inventory Item</a></a></div> <div align="left" style="margin-left:24px;"> <h2>Inventory list</h2> <?php echo $product_list; ?> </div> <hr /> <a name="inventoryForm" id="inventoryForm"></a> <h3> ↓Add New Inventory Item Form ↓ </h3> <form action="inventory_list.php" enctype="multipart/form-data" name="myForm" id="myForm" method="post"> <table width="90%"border="0" cellspacing="0" cellpadding="6"> <tr> <td width="20%" align="right">Product Name</td> <td width="80%"><label> <input name="product_name" type="text" id="product_name" size="64" /> </label></td> </tr> <tr> <td align="right">Product Price</td> <td><label> $ <input name="price" type="text" id="price" size="12" /> </label></td> </tr> <tr> <td align="right">Category</td> <td><label> <select name="category" id="category"> <option value=""></option> <option value="Gold">Gold</option> <option value="Platinum">Platinum</option> <option value="Silver">Silver</option> </select> </label></td> </tr> <tr> <td align="right">Subcategory</td> <td><select name="subcategory" id="subcategory"> <option value=""></option> <option value="Bracelets">Bracelets</option> <option value="Earings">Earings</option> <option value="Estate/Antique">Estate/Antique</option> <option value="Necklace">Necklace</option> <option value="Pendents">Pendents</option> <option value="Rings">Rings</option> <option value="Watches">Watches</option> </select></td> </tr> <tr> <td align="right">Product Details</td> <td><label> <textarea name="details" type="details" cols="64" rows="5"></textarea> </label></td> </tr> <tr> <td align="right">Product Image</td> <td><label> <input type="file" name="fileField" value="fileField" /> </label></td> </tr> <tr> <td> </td> <td align="left"><label> <input type="submit" name="button" id="button" value="SUBMIT ITEM" /> </label></td> </tr> </table> </form> <h3><br /> </h3> </div> <?php include_once("../template_footer.php");?> </div> </body> </html>
-
How would i go about fixing it. i didnt get that error when i run inventory_list, only the inventory_edit. they both have the same code
-
i just created a inventory_edit page for my inventory_list. The page runs, but at the top of the page i get a message: Notice: Undefined variable: product_list in /home6/modeljew/public_html/storeadmin/inventory_edit.php on line 85 inventory_edit.php <?php session_start(); if (!isset($_SESSION["manager"])) { header("location: admin_login.php"); exit(); } // Be sure to check that this manager SESSION value is in fact in the database $managerID = preg_replace('#[^0-9]#i', '', $_SESSION["id"]); // filter everything but numbers and letters $manager = preg_replace('#[^A-Za-z0-9]#i', '', $_SESSION["manager"]); // filter everything but numbers and letters $password = preg_replace('#[^A-Za-z0-9]#i', '', $_SESSION["password"]); // filter everything but numbers and letters // Run mySQL query to be sure that this person is an admin and that their password session var equals the database information // Connect to the MySQL database include "../storescripts/connect_to_mysql.php"; $sql = mysql_query("SELECT * FROM admin WHERE id='$managerID' AND username='$manager' AND password='$password' LIMIT 1"); // query the person // ------- MAKE SURE PERSON EXISTS IN DATABASE --------- $existCount = mysql_num_rows($sql); // count the row nums if ($existCount == 0) { // evaluate the count echo "Your login session data is not on record in the database."; exit(); } ?> <?php // Script Error Reporting error_reporting(E_ALL); ini_set('display_errors','1'); ?> <?php // Parse the form data and add inventory item to the system if (isset($_POST['product_name'])) { $pid = mysql_real_escape_string($_POST['thisID']); $product_name = mysql_real_escape_string($_POST['product_name']); $price = mysql_real_escape_string($_POST['price']); $category = mysql_real_escape_string($_POST['category']); $subcategory = mysql_real_escape_string($_POST['subcategory']); $details = mysql_real_escape_string($_POST['details']); // See if that the product name is an identical match to another product in the system $sql = mysql_query("UPDATE products SET product_name='$product_name',price='$price',details='$details',category='$category',subcategory='$subcategory'WHERE id='$pid'"); if ($_FILES['fileField']['tmp_name'] != ""){ // Place image in the folder $newname = "$pid.jpg"; move_uploaded_file($_FILES['fileField']['tmp_name'], "../inventory_images/$newname"); } header("location: inventory_list.php"); exit(); } ?> <?php // Gather this products full information for inserting automatically into the edit form below on page if (isset($_GET['pid'])) { $targetID = $_GET['pid']; $sql = mysql_query("SELECT * FROM products WHERE id='$targetID' LIMIT 1"); $productCount = mysql_num_rows($sql); //count the output amount if ($productCount > 0) { while($row = mysql_fetch_array($sql)){ $id = $row["id"]; $product_name = $row["product_name"]; $price = $row["price"]; $category = $row["category"]; $subcategory = $row["subcategory"]; $details = $row["details"]; $date_added = strftime("%b %d, %Y", strtotime($row["date_added"])); } } else { echo "Sorry that doesnt exist."; exit(); } } ?> <!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>Inventory List</title> <link rel="stylesheet" href="../style/style.css" type="text/css" media="screen" /> </head> <body> <div align="center" id="mainWrapper"> <?php include_once("../template_header.php");?> <div id="pageContent"><br /> <div align="right" style="margin-right:32px;"><a href="inventory_list.php#inventoryForm">+ Add New Inventory Item</a></a></div> <div align="left" style="margin-left:24px;"> <h2>Inventory list</h2> <?php echo $product_list; ?> </div> <hr /> <a name="inventoryForm" id="inventoryForm"></a> <h3> ↓Add New Inventory Item Form ↓ </h3> <form action="inventory_edit.php" enctype="multipart/form-data" name="myForm" id="myForm" method="post"> <table width="90%"border="0" cellspacing="0" cellpadding="6"> <tr> <td width="20%" align="right">Product Name</td> <td width="80%"><label> <input name="product_name" type="text" id="product_name" size="64" value="<?php echo $product_name; ?>" /> </label></td> </tr> <tr> <td align="right">Product Price</td> <td><label> $ <input name="price" type="text" id="price" size="12" value="<?php echo $price; ?>" /> </label></td> </tr> <tr> <td align="right">Category</td> <td><label> <select name="category" id="category"> <option value=""></option> <option value="<?php echo $category; ?>"><?php echo $category; ?></option> <option value="Gold">Gold</option> <option value="Platinum">Platinum</option> <option value="Silver">Silver</option> </select> </label></td> </tr> <tr> <td align="right">Subcategory</td> <td><select name="subcategory" id="subcategory"> <option value=""></option> <option value="<?php echo $subcategory; ?>"><?php echo $subcategory; ?></option> <option value="Bracelets">Bracelets</option> <option value="Earings">Earings</option> <option value="Estate/Antique">Estate/Antique</option> <option value="Necklace">Necklace</option> <option value="Pendents">Pendents</option> <option value="Rings">Rings</option> <option value="Watches">Watches</option> </select></td> </tr> <tr> <td align="right">Product Details</td> <td><label> <textarea name="details" type="details" cols="64" rows="5"><?php echo $details; ?></textarea> </label></td> </tr> <tr> <td align="right">Product Image</td> <td><label> <input type="file" name="fileField" value="fileField" /> </label></td> </tr> <tr> <td> </td> <td align="left"><label> <input name="thisID" type="hidden" value="<?php echo $targetID ?>" /> <input type="submit" name="button" id="button" value="Make Changes" /> </label></td> </tr> </table> </form> <h3><br /> </h3> </div> <?php include_once("../template_footer.php");?> </div> </body> </html>