当我尝试在浏览器中运行此 PHP 文件时,我收到错误消息:
解析错误:语法错误,意外')',期待 :: (T_PAAMAYIM_NEKUDOTAYIM)" 在第 50 行
我在第 54 行和第 55 行也有我无法摆脱的红色。对于这两行,错误工具提示说
语法错误:预期:::,(
据我所知,我所有的花括号都在他们需要的地方。我不知道怎么了。有任何想法吗?这是代码:
<?php
// If a session is NOT already in progress for this
// user, start one:
if(!session_id()){
session_start();
}
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Shopping Cart Manager</title>
<link href="css/minismall.css" rel="stylesheet">
</head>
<body>
<?php
//Check if session cart variable exists and set shortcut:
if (isset($_SESSION['cart'])) {
$cart = $_SESSION['cart'];
}
else { // $_SESSION['cart'] does NOT exist
$cart = Array();
}
include 'catalog.php';
// Check if ‘remove’ form field exists and set shortcut:
if (isset($_POST['remove'])) {
$remove = $_POST['remove'];
}
else { //$_POST['remove'] does NOT exist
$remove = Array();
}
// Initialize $totalPrice, $prodIDStr, and session ‘numItems’
// variables to either 0 or empty string as appropriate:
$totalPrice = 0;
$prodIDStr = "";
$_SESSION[numItems] = 0; // Did I initialize this session
// variable correctly?
//$_SESSION['numItems'] = $numItems;
// Loop through each form field (this page is called
// from catalog.php):
// If a form field’s value (product quantity) is
// a positive number (nonzero), is NOT the submit
// button, AND the remove checkbox for removing this
// product has NOT been checked (do these checks in one
// condition):
while (list($productID, $qty) = each($_POST)){
if (($qty > 0) && (type != submit) && !isset(checkbox)){ // LINE 50
$cat[$productID]['qty'] += $qty; // Update the
// cart's element for this product with
// the product quantity from the form
} //LINE 54
Else if (($qty = 0) || isset(checkbox)){ // If the
// product's quantity is zero or the product's
// remove checkbox is checked
// Remove this product from the cart array:
//unset($_SESSION['cart'][$productID]);
unset($cart['productID']['qty']);
}//END OF Else if
}//END OF while loop
// Connect to your database server and select your database:
require 'dbConnect.php';
// Loop through your cart array (foreach productID's
// quantity in cart):
$_SESSION['numItems'] = $cart; // Update the number of
// items in your cart
// Build a comma-delimited string in $prodIDStr containing the
// product IDs of the products currently in our cart array:
$prodIDStr = implode("productID, qty", $cart);
if($_SESSION[numItems] = 0){ // If cart is empty:
print "<h3>Your shopping cart is empty!</h3>\n";
}
Else { // If cart is not empty:
// Remove trailing comma from $prodIDstr:
// Query the database for all products in our cart
// using the $prodIDStr ordering them
// by category and then by productID:
try {
$prodIDStr = $pdo->query("SELECT * FROM $cart ORDER BY $cat AND $productID");
}
catch (PDOException $e) {
$error = "Error fetching product info for category $cat: " . $e->getMessage();
include 'error.html.php';
exit();
}
// Display beginning of form which calls cart_yourname.php
// when submitted using the POST method:
?>
<form action="cart_speterson86.php" method="post">
<table>
<tr class="header">
<th>Remove</th>
<th>Image</th>
<th>Description</th>
<th>Price - US$</th>
<th>Subtotal</th>
<th>Quantity</th>
<th style="background-color: #fff"> </th>
</tr>
<?php
//
// Step through result set of products in this category
// and display each product in its own table row
//
while ($row = $itemsResult->fetch()) {
// Convert any special HTML characters to their HTML
// entities. Example: & --> &
//
// Also, strip out any HTML tags found in the data
$remove = htmlspecialchars(strip_tags($row['loc']));
$imageLocation = htmlspecialchars(strip_tags($row['loc']));
$desc = htmlspecialchars(strip_tags($row['description']));
$price = htmlspecialchars(strip_tags($row['price']));
$subTotal = htmlspecialchars(strip_tags($row['loc']));
$price = "\$" . number_format($price, 2);
$productID = strip_tags($row['prodid']);
//
// Set $qty to contain what is in our session cart array.
// If your session cart array element for this product is
// empty, set the $qty to its default value of 0.
//
if (isset($_SESSION['cart'][$productID])) {
$qty = $_SESSION['cart'][$productID];
}
else {
$qty = 0;
}
// Build and display next product row in table
print <<<TABROW
<tr>
<td><input type="checkbox" name="$remove[$productID]" value="1"></td>
<td><img src="$imageLocation" alt="groovy product $desc"></td>
<td class="desc">$desc</td>
<td class="price">$price</td>
<td class="price">$subTotal</td>
<td class="qty">
<label for="quantityForProduct$productID">Qty</label>
<input type="text"
name="$productID"
id="quantityForProduct$productID"
value="$qty"
size="3">
</td>
</tr>
TABROW;
} // end while another row in product result set
?>
<tr>
<td colspan="4" id="submitCell">
<input type="submit" name="addCart" value="Add Items to Cart">
</td>
<td><input type="submit" name="checkOut" value="Checkout"></td>
<td><input type="submit" name="updateCart" value="Update Cart"></td>
</tr>
</table>
</form>
<br>
<?php
} // END OF Else cart is not empty
?>
</body>
</html>