Shopping cart calculate the amount

Hi,
I have a shopping cart (first script)below is OK, but when I want to make the calculate(second script) the amount,
it indicate the under last line ( <p><a href=“index.php”>Home</a></p), error is: Parse error: syntax error, unexpected $end in C:\wampserver32\www\sale\votre_panier.php on line 41
I wish someone can help me to modifiy it.

first script (it is OK)


<?php
session_start();
require_once 'panier.php';
$panier = new Panier('produits');
$listproduit = $panier->getPanier();
?>
<?php if(!$listproduit){?>
<p> votre panier est vide </p>
<?php } else {?>
<table border="1" width="50%">
<tr>
<td>Name</td>
<td>Price</td>
<td>Quantity</td>
<td>Amount</td>
</tr>
<?php foreach($listproduit as $produit) { ?>
<tr>
<td><?php print $produit['name'] ?></td>
<td><?php print $produit['price'] ?></td>
<td><?php print $produit['quantity'] ?></td> 
</tr> 
<?php } ?>
</table>
<?php } ?>
<p><a href="index.php">Home</a></p>

second script:


<?php
session_start();
require_once 'panier.php';
$panier = new Panier('produits');
$listproduit = $panier->getPanier();
?>
<?php if(!$listproduit){?>
<p> votre panier est vide </p>
<?php } else {?>
<table border="1" width="50%">
<tr>
<td>Name</td>
<td>Price</td>
<td>Quantity</td>
<td>Amount</td>
</tr>

<?php
if(isset($_SESSION['cart'])){
$sql="SELECT * FROM trybase WHERE name IN (";
foreach($_SESSION['cart'] as $id =>$value) {
$sql.=$id.",";
}
$sql=substr($sql, 0, -1).") ORDER BY name ASC";
$query=mysql_query($sql);
while ($produit=mysql_fetch_array($query)){
?>

<?php foreach($listproduit as $produit) { ?>
<tr>
<td><?php print $produit['name'] ?></td>
<td><?php print $produit['price'] ?></td>
<td><?php print $produit['quantity'] ?></td> 
<td><?php print $_SESSION['cart'][$produit['name']]*$produit['quantity']?>&#8364;</td> 
</tr> 
<?php } ?>
</table>
<?php } ?>
<p><a href="index.php">Home</a></p>

Problem is that you’ve got two (as I count it) more loop openers { than you have closing ones. So the PHP processor doesn’t understand why the file ends before you close them.


<?php
session_start();
require_once 'panier.php';
$panier = new Panier('produits');
$listproduit = $panier->getPanier();
?>
<?php if(!$listproduit){?> // open  a { - 1 running total
<p> votre panier est vide </p>
<?php } else {?> // close 1, open another, 1 r/t
<table border="1" width="50%">
<tr>
<td>Name</td>
<td>Price</td>
<td>Quantity</td>
<td>Amount</td>
</tr>

<?php
if(isset($_SESSION['cart'])){  // open another, 2 r/t
$sql="SELECT * FROM trybase WHERE name IN (";
foreach($_SESSION['cart'] as $id =>$value) { // open another, 3 r/t
$sql.=$id.",";
}  // close one, 2 r/t
$sql=substr($sql, 0, -1).") ORDER BY name ASC";
$query=mysql_query($sql);
while ($produit=mysql_fetch_array($query)){  // open another, 3 r/t
?>

<?php foreach($listproduit as $produit) { ?>  // open another, 4 r/t
<tr>
<td><?php print $produit['name'] ?></td>
<td><?php print $produit['price'] ?></td>
<td><?php print $produit['quantity'] ?></td>
<td><?php print $_SESSION['cart'][$produit['name']]*$produit['quantity']?>&#8364;</td>
</tr>
<?php } ?>  // close one, 3 r/t
</table>
<?php } ?>  // close another, 2 r/t
<p><a href="index.php">Home</a></p>

Still two open braces that you don’t close, unless there is more to the page. When the error message gives a problem for the last line of your code, it’s often something you opened but didn’t close.

Also you need to keep in mind that the mysql_* extension is now deprecated as of the current version of PHP and will very likely be removed from the next 5.x version and will likely not be in PHP 6.x (when it eventually is released). You should migrate over to either the mysqli_* extension or to PDO. PDO is a better choice as it doesn’t tie you down so much to a particular database server software.

Once you have migrated you should use Prepared Statements to prevent SQL Injection attacks. Have a read of this article from the PHP manual, it shows how to use prepared statements with PDO and also explains the principle.

When you’re using { } with code blocks, try indenting for each { used, it helps when spotting where you may have missed any }