jigsawsoul Posted March 28, 2010 Share Posted March 28, 2010 Hey guy's i keep getting this error message below when running this script can any one tell me where i'm going wrong, what i should change please. thanks... Error Message Parse error: syntax error, unexpected T_IF in /home/jigsaws2/public_html/project/public/checkout.php on line 14 Checkout.php <?php session_start(); include('../resources/config.php'); include($config["paths"]["resources"] . 'opendb.php'); include($config["paths"]["resources"] . '_library/login.php'); include($config["paths"]["resources"] . '_library/cart.php'); $content = ' <h2>Checkout</h2> '. if(isset($_SESSION['cart'])) { .' <form action="index.php?view=update_cart" method="post"> <table id="items"> <thead> <tr> <th>Item</th> <th>Item Price</th> <th>Qty</th> <th>Subtotal</th> </tr> </thead> <tbody> '. foreach($_SESSION['cart'] as $id => $qty): $product = find_product($id); .' <tr> <td>'. echo $product['title']; .'</td> <td>$'. echo number_format($product['price'],2); .'</td> <td><input type="text" size="2" name="'. echo $id .'" maxlength="2" value="'. echo $qty .'" /></td> <td>$'. echo number_format($product['price'] * $qty, 2); .'</td> </tr> '. endforeach; .' </tbody> </table> <p><input type="submit" name="update" value="update" /></p> </form> '. } else { echo '<p>your cart is empty... <a href="index.php">continue shopping</a></p>'; } .' '; include($config["paths"]["resources"] . 'closedb.php'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/196817-error-parse-error-syntax-error-unexpected-t_if-help-please/ Share on other sites More sharing options...
GetPutDelete Posted March 28, 2010 Share Posted March 28, 2010 Just before the if() you need to end the line with a ; Unless you are using shorthand you can't follow a string into an if() loop. So for example... $content = ' <h2>Checkout</h2>'; if(isset($_SESSION['cart'])) { $content.= '<form action="index.php?view=update_cart" method="post">......... Quote Link to comment https://forums.phpfreaks.com/topic/196817-error-parse-error-syntax-error-unexpected-t_if-help-please/#findComment-1033192 Share on other sites More sharing options...
teamatomic Posted March 28, 2010 Share Posted March 28, 2010 Check your quotes. Singles within singles. '. if(isset($_SESSION['cart'])) HTH Teamatomic Quote Link to comment https://forums.phpfreaks.com/topic/196817-error-parse-error-syntax-error-unexpected-t_if-help-please/#findComment-1033193 Share on other sites More sharing options...
jigsawsoul Posted March 28, 2010 Author Share Posted March 28, 2010 @teamatomic, what do you mean i've always written my sessions like this?, i'm new is there a problem with them? @GetPutDelete, i tried what you said i have this error message though now Error Message: Parse error: syntax error, unexpected ';' in /home/jigsaws2/public_html/project/public/checkout.php on line 14 Checkout.php <?php session_start(); include('../resources/config.php'); include($config["paths"]["resources"] . 'opendb.php'); include($config["paths"]["resources"] . '_library/login.php'); include($config["paths"]["resources"] . '_library/cart.php'); $content = ' <h2>Checkout</h2> '.; if(isset($_SESSION['cart'])) { .' <form action="index.php?view=update_cart" method="post"> <table id="items"> <thead> <tr> <th>Item</th> <th>Item Price</th> <th>Qty</th> <th>Subtotal</th> </tr> </thead> <tbody> '. foreach($_SESSION['cart'] as $id => $qty): $product = find_product($id); .' <tr> <td>'. $product['title']; .'</td> <td>$'. number_format($product['price'],2); .'</td> <td><input type="text" size="2" name="'. $id .'" maxlength="2" value="'. $qty .'" /></td> <td>$'. number_format($product['price'] * $qty, 2); .'</td> </tr> '. endforeach; .' </tbody> </table> <p><input type="submit" name="update" value="update" /></p> </form> '. } else { echo '<p>your cart is empty... <a href="index.php">continue shopping</a></p>'; } .' '; include($config["paths"]["resources"] . 'closedb.php'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/196817-error-parse-error-syntax-error-unexpected-t_if-help-please/#findComment-1033200 Share on other sites More sharing options...
premiso Posted March 28, 2010 Share Posted March 28, 2010 You are concatenating if's and foreaches into your code which is not proper syntax...in any language that I know. I would read up on proper syntax usage. In the mean time this is how it should look: (at least one way) <?php session_start(); include('../resources/config.php'); include($config["paths"]["resources"] . 'opendb.php'); include($config["paths"]["resources"] . '_library/login.php'); include($config["paths"]["resources"] . '_library/cart.php'); $content = '<h2>Checkout</h2>'; if(isset($_SESSION['cart'])) { $content .= ' <form action="index.php?view=update_cart" method="post"> <table id="items"> <thead> <tr> <th>Item</th> <th>Item Price</th> <th>Qty</th> <th>Subtotal</th> </tr> </thead> <tbody>'; foreach($_SESSION['cart'] as $id => $qty){ // keep sytnax similar, use one or the other (the foreach () : endforeach / if : endif; or { } $product = find_product($id); $content .= ' <tr> <td>'. $product['title'] .'</td> <td>$'. number_format($product['price'],2) .'</td> <td><input type="text" size="2" name="'. $id .'" maxlength="2" value="'. $qty .'" /></td> <td>$'. number_format($product['price'] * $qty, 2) .'</td> </tr>'; } $content .= ' </tbody> </table> <p><input type="submit" name="update" value="update" /></p> </form>'; echo $content; }else { echo '<p>your cart is empty... <a href="index.php">continue shopping</a></p>'; } include($config["paths"]["resources"] . 'closedb.php'); ?> It seems like you were combining different syntax which I bet you picked up from some templates. Like I said, read up on proper syntax, look what I fixed and why it was that way. Note that I changed the foreach () : to foreach () { since you are using if () { you should keep it similar so either change the if to use the endif; format like the endforeach;. It is better not to mix and match them as it causes confusion for some people. Quote Link to comment https://forums.phpfreaks.com/topic/196817-error-parse-error-syntax-error-unexpected-t_if-help-please/#findComment-1033201 Share on other sites More sharing options...
GetPutDelete Posted March 28, 2010 Share Posted March 28, 2010 You've kept the concatenation operators in (Period operator) when you should have removed them as the line had ended with a ; Use the above example by premiso it's all done for you. Quote Link to comment https://forums.phpfreaks.com/topic/196817-error-parse-error-syntax-error-unexpected-t_if-help-please/#findComment-1033204 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.