Jump to content

shopping cart calculate the amount


Louffeman

Recommended Posts

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[/url]</p), error is: Parse error: syntax error, unexpected $end in C:wampserver32wwwsalevotre_panier.php on line 41I wish someone can help me to modifiy it.first script (it is OK)<?phpsession_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[/url]</p>second script: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><?phpif(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']?></td></tr><?php } ?></table><?php } ?><p><a href="index.php">Home[/url]</p>

Link to comment
Share on other sites

You are missing a closing bracket } for:

 

if(isset($_SESSION['cart'])){

and

 

<?php } else {?>

 

Also you haven't put a semicolon at the end of these 4 PHP lines:

 

<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']?></td>
Link to comment
Share on other sites

I didn't put the semicolon in my first script, but it runs well.now I put the semicolon, it change nothing, so I think it's not a problem.

 

<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']?></td>

 

for this line, I put serveral places to teste it, the error is always:it mean's the "else" is not necessary, No ? I d'ont know.Parse error: syntax error, unexpected T_ELSE in C:wampserver32wwwsalevotre_panier.php on line 41
<?php } else {?>
Also you haven't put a semicolon at the end of these 4 PHP lines:
Link to comment
Share on other sites

yes, the server have no error, but not the display of the buying object (name, price, quantity and amount)which mean's this part doesn't work:

<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'];?>€</td></tr>

Link to comment
Share on other sites

Well you are overwriting the $produit variable from the database.

Try removing the foreach loop.

 

Change

 

 

<?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']?></td></tr><?php } ?>

 

to

 

 

<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']?></td></tr>
Link to comment
Share on other sites

it is clear that this part has problem, but the server has no error indication, <?phpif(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)){?><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']?>€</td></tr><?php } ?><?php } ?><?php } ?>

Link to comment
Share on other sites

it is clear that this part has problem, but the server has no error indication,

<?phpif(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)){?><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']?>€</td></tr><?php } ?><?php } ?><?php } ?>

Link to comment
Share on other sites

I did as you sais,but it is still the same, no display of the buying object (name, price, quantity and amount)<?phpif(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);echo mysql_num_rows($query).'<br>';while ($produit=mysql_fetch_array($query)){?>

Link to comment
Share on other sites

yes, this is the panier.php<?phpclass Panier{public $nom;private static $nbPanier = 0;public function __construct($nom){$this->name = $nom;}public function set ($key, $value){$_SESSION['paniers'][$this->nom][$key] = $value;}public function get($key){if(isset($_SESSION['paniers'][$this->nom][$key]))return $_SESSION['paniers'][$this->nom][$key];return null;}public function delet($key){if(isset($_SESSION['paniers'][$this->nom][$key]))unset($_SESSION['paniers'][$this->nom][$key]);}public function getPanier(){if(isset($_SESSION['paniers'][$this->nom]))return $_SESSION['paniers'][$this->nom];return array();}public function clear(){if(isset($_SESSION['paniers'][$this->nom]))unset($_SESSION['paniers'][$this->nom]);}}?>

Link to comment
Share on other sites

Also in paniers.php, change

$this->name = $nom;

to

$this->nom = $nom;

and change

foreach($_SESSION['paniers'] as $id =>$value) {$sql.=$id.",";}

to

foreach($_SESSION['paniers'] as $name=>$value) {$sql.="'".$name."',";}
Edited by JamesB
Link to comment
Share on other sites

I have already changed<?phpsession_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><?phpprint_r($_SESSION);if(isset($_SESSION['paniers'])){$sql="SELECT * FROM trybase WHERE name IN (";foreach($_SESSION['paniers'] as $id =>$value) {$sql.=$id.",";}$sql=substr($sql, 0, -1).") ORDER BY name ASC";$query=mysql_query($sql);while ($produit=mysql_fetch_array($query)){?><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'];?>€</td></tr><?php } ?><?php } ?><?php } ?></table><p><a href="index.php">Home[/url]</p>

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...