Louffeman Posted August 18, 2014 Share Posted August 18, 2014 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 More sharing options...
JamesB Posted August 18, 2014 Share Posted August 18, 2014 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 More sharing options...
Louffeman Posted August 18, 2014 Author Share Posted August 18, 2014 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 More sharing options...
JamesB Posted August 18, 2014 Share Posted August 18, 2014 Okay forget the semilcolon. Try changing: </tr><?php } ?></table><?php } ?><p><a href="index.php">Home[/url]</p> to: </tr><?php } ?><?php } ?><?php } ?></table><?php } ?><p><a href="index.php">Home[/url]</p> Link to comment Share on other sites More sharing options...
Louffeman Posted August 18, 2014 Author Share Posted August 18, 2014 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 More sharing options...
JamesB Posted August 18, 2014 Share Posted August 18, 2014 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 More sharing options...
Louffeman Posted August 18, 2014 Author Share Posted August 18, 2014 after having cancelled this line:<?php foreach($listproduit as $produit) { ?>the result is the same,no display for the buying object (name, price, quantity and amount) Link to comment Share on other sites More sharing options...
Louffeman Posted August 18, 2014 Author Share Posted August 18, 2014 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 More sharing options...
Louffeman Posted August 18, 2014 Author Share Posted August 18, 2014 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 More sharing options...
JamesB Posted August 18, 2014 Share Posted August 18, 2014 Check how many rows are returned in your query $query=mysql_query($sql);echo mysql_num_rows($query).'<br>'; Link to comment Share on other sites More sharing options...
Louffeman Posted August 18, 2014 Author Share Posted August 18, 2014 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 More sharing options...
JamesB Posted August 18, 2014 Share Posted August 18, 2014 When u run the file, which number is being displayed on the page? a zero? Link to comment Share on other sites More sharing options...
Louffeman Posted August 18, 2014 Author Share Posted August 18, 2014 you can see this picture:it is the resulthttp://www.hostingpics.net/viewer.php?id=540557php.jpgI think it is this line in question,but I d'ont know how to modifier,$sql=substr($sql, 0, -1).") ORDER BY name ASC"; Link to comment Share on other sites More sharing options...
JamesB Posted August 18, 2014 Share Posted August 18, 2014 That would suggest to me that this line is evaluating to false: if(isset($_SESSION['cart'])){ try debugging your session by adding: print_r($_SESSION);if(isset($_SESSION['cart'])){ Link to comment Share on other sites More sharing options...
Louffeman Posted August 18, 2014 Author Share Posted August 18, 2014 the picture shows the array result is correct, the quantity that I put is 2, OKhttp://www.hostingpics.net/viewer.php?id=450536php1.jpg Link to comment Share on other sites More sharing options...
JamesB Posted August 18, 2014 Share Posted August 18, 2014 I don't see 'cart' in that output. So $_SESSION['cart'] is not set. Link to comment Share on other sites More sharing options...
Louffeman Posted August 18, 2014 Author Share Posted August 18, 2014 because I copied this script from a tutorial,so I d'ont know 'cart' is what,can you explain me ? it is a variable ? Link to comment Share on other sites More sharing options...
JamesB Posted August 18, 2014 Share Posted August 18, 2014 yeh basically. $_SESSION is an array. $_SESSION['cart'] is an element in that array. So somewhere in your code a value should be assigned to $_SESSION['code']. Can you post the code in panier.php? Link to comment Share on other sites More sharing options...
Louffeman Posted August 18, 2014 Author Share Posted August 18, 2014 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 More sharing options...
JamesB Posted August 18, 2014 Share Posted August 18, 2014 change if(isset($_SESSION['cart'])){ to if(isset($_SESSION['paniers'])){ you forgot to rename one Link to comment Share on other sites More sharing options...
Louffeman Posted August 18, 2014 Author Share Posted August 18, 2014 so, now, the error is in the line 29:hile ($produit=mysql_fetch_array($query)){http://www.hostingpics.net/viewer.php?id=178851php2.jpg Link to comment Share on other sites More sharing options...
Louffeman Posted August 18, 2014 Author Share Posted August 18, 2014 but I saw the tutorial is:if(isset($_SESSION['cart'])){ Link to comment Share on other sites More sharing options...
JamesB Posted August 18, 2014 Share Posted August 18, 2014 Rename your other 2 occurrences of $_SESSION['cart'] to $_SESSION['paniers'] Link to comment Share on other sites More sharing options...
JamesB Posted August 18, 2014 Share Posted August 18, 2014 (edited) 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 August 18, 2014 by JamesB Link to comment Share on other sites More sharing options...
Louffeman Posted August 18, 2014 Author Share Posted August 18, 2014 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now