我正在制作一个网站,上面有产品。我遇到了一个意外的文件结尾错误。这是整个代码。我还有一个页面正在运行这两个功能,同时需要这个我称之为"cart"的页面。
<?php
include('inc/connect.php');
session_start();
?>
<html>
<head>
<title>Cart</title>
<link rel='stylesheet' href='css/main.css' />
</head>
<body>
<?php
$page = 'index.php';
if(isset($_GET['add'])){
$add_id = (int)$_GET['add'];
$quantity = mysql_query("SELECT id, quantity FROM products WHERE id='$add_id'");
while($quantity_row = mysql_fetch_assoc($quantity)){
if($quantity_row['quantity'] !=@$_SESSION['cart_'.(int)$add_id]){
@$_SESSION['cart_'.(int)$_GET['add']]+='1';
}
}
}
function products(){
$get = mysql_query("SELECT id, name, description, price FROM products WHERE quantity > 0 ORDER BY id DESC");
if(mysql_num_rows($get) == 0){
echo "There are no products to display.";
}
else{
while($get_row = mysql_fetch_assoc($get)){
echo '<p>'.$get_row['name'].'<br />'.$get_row['description'].'<br />'.$get_row['price'].' <a href="cart.php?add='.$get_row['id'].'">Add</a></p>';
}
}
}
function cart(){
foreach($_SESSION as $name => $value){
if($value>0){
if(substr($name, 0, 5) == 'cart_'){
$id = mysql_real_escape_string(substr($name, 5, (strlen($name)-5)));
$get = mysql_query("SELECT id, name, price FROM products WHERE id='$id'");
while($get_row = mysql_fetch_assoc($get)){
$sub = $get_row['price']*$value;
echo $get_row['name'].' x '.$value.' @ $'.number_format($get_row['price'], 2).' = $'.number_format($sub, 2);
}
}
else{
echo "Your cart is empty";
}
}
}
?>
</body>
</html>
while循环缺少一个右括号:
while($get_row = mysql_fetch_assoc($get)){
$sub = $get_row['price']*$value;
echo $get_row['name'].' x '.$value.' @ $'.number_format($get_row['price'], 2).' = $'.number_format($sub, 2);
您的最后一个函数永远不会关闭。如果您正确地缩进它(PHP是如何读取它的):
function cart(){
foreach($_SESSION as $name => $value){
if($value>0){
if(substr($name, 0, 5) == 'cart_'){
$id = mysql_real_escape_string(substr($name, 5, (strlen($name)-5)));
$get = mysql_query("SELECT id, name, price FROM products WHERE id='$id'");
while($get_row = mysql_fetch_assoc($get)){
$sub = $get_row['price']*$value;
echo $get_row['name'].' x '.$value.' @ $'.number_format($get_row['price'], 2).' = $'.number_format($sub, 2);
}
}
else{
echo "Your cart is empty";
}
}
}
//????
您似乎忘记关闭while
语句。