Jump to content

cart.php


CASHOUT

Recommended Posts

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>

 

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/244276-cartphp/
Share on other sites

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>

Link to comment
https://forums.phpfreaks.com/topic/244276-cartphp/#findComment-1254640
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.