我在PHP代码中有一个意外的文件结尾?

时间:2013-02-15 21:49:40

标签: php file

我正在建立一个包含产品的网站。我遇到了意外的文件结束错误。这是整个代码。我还有一个运行这两个功能的页面,同时需要这个名为'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>

2 个答案:

答案 0 :(得分: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声明。

答案 1 :(得分:2)

你错过了这个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);