php文件中的语法错误添加到购物车

时间:2014-04-21 18:09:46

标签: php

我在以下php文件中遇到各种错误(语法)。目前的php文件是关于onlineshops产品到购物车添加功能。我收到各种错误。错误从第一个echo开始,并列为 syntax error, unexpected 'echo'

如果我删除它,我会在下一行收到 syntax error, unexpected ';'

任何帮助都会很棒。感谢。

/* display cart */

if ( isset( $_SESSION['cart'] ) )
        (
        echo "<table border=0 cellspacing=0 cellpadding=0 width='500'>";
        $total = 0;
        foreach($_SESSION['cart'] as $id => $x)
        (
        $result = mysql_query("SELECT * from onlineshop WHERE id=$id",$db);
        $myrow = mysql_fetch_array($result);     
        $name = $myrow['name'];
        $name = substr($name,0,40);
        $price = $myrow['price'];
        $line_cost = $price *  $x;
        $total = $total + $line_cost;

         echo "<tr>";
         echo "<td allign='left'> $name </td>";
         echo "<td align = 'right'>X $x <a href='cart.php?id=".$id."&action=remove'> reduce </a></td>";
         echo "<td align = 'right'>= $line_cost £";
         echo "</tr>";
        )
        echo "<tr>";
        echo "<td align ='right'><br>Total=</td>";
        echo "<td align ='right'><b><br> $total £</b></td>";
        echo "</tr>";
        echo "</table>";
    )
    else
        echo "Cart is empty";

2 个答案:

答案 0 :(得分:1)

你使用错误的牙套。将()替换为{}语句中的ifforeach循环:

if (isset($_SESSION['cart'])){
    echo "<table border=0 cellspacing=0 cellpadding=0 width='500'>";
    $total = 0;
    foreach($_SESSION['cart'] as $id => $x){
        $result = mysql_query("SELECT * from onlineshop WHERE id=$id",$db);
        $myrow = mysql_fetch_array($result);     
        $name = $myrow['name'];
        $name = substr($name,0,40);
        $price = $myrow['price'];
        $line_cost = $price *  $x;
        $total = $total + $line_cost;

        echo "<tr>";
        echo "<td allign='left'> $name </td>";
        echo "<td align = 'right'>X $x <a href='cart.php?id=".$id."&action=remove'> reduce </a></td>";
        echo "<td align = 'right'>= $line_cost £";
        echo "</tr>";
    }
    echo "<tr>";
    echo "<td align ='right'><br>Total=</td>";
    echo "<td align ='right'><b><br> $total £</b></td>";
    echo "</tr>";
    echo "</table>";
}else
    echo "Cart is empty";

答案 1 :(得分:0)

对于{}或循环内容块,您应该使用()大括号而不是if

if (isset($_SESSION['cart'])) {
//....
}

而不是

if (isset($_SESSION['cart'])) (
//....
)

foreach循环相同。