1

I'm getting various errors (syntax) on the following php file. The current php file is about onlineshops product-to-cart adding function. I get various errors. The errors start from the first echo and are listed as syntax error, unexpected 'echo'.

If I remove it, I get a syntax error, unexpected ';' on the next line.

Any help will be great. Thanks.

/* 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";
Eugene
  • 4,352
  • 8
  • 55
  • 79
cmario
  • 83
  • 2
  • 12

2 Answers2

1

You use wrong braces. Replace () with {} in if statement and foreach loop :

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";
potashin
  • 44,205
  • 11
  • 83
  • 107
0

You should use {} braces instead of () for if's or loop's content block.

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

instead of

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

And the same to foreach loop.

Jakub Matczak
  • 15,341
  • 5
  • 46
  • 64