KevinM1 Posted December 12, 2006 Share Posted December 12, 2006 Anyone mind helping me figure out what's causing the following syntax error?[quote]Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/thinkin8/public_html/viewitem.php on line 46[/quote]The code in question is:[code]<?php#viewitem.php scriptinclude('../php_config/config.php');include('../dbconnect.php');session_start();ob_start();if(!isset($_SESSION['cartInfo'])){ $myCart = new ShoppingCart();}else{ $myCart = urldecode(unserialize($_SESSION['cartInfo']));}if(isset($_GET['cat'])){ $tableName = $_GET['cat'];}else{ header("Location: www.thinkingmachinestore.com");}if(isset($_GET['id'])){ $id = $_GET['id'];}else{ header("Location: www.thinkingmachinestore.com");}$query = "SELECT * FROM $tableName WHERE {$tableName}_id = '$id'";$result = mysql_query($query);if(mysql_num_rows == 1){ $row = mysql_fetch_assoc($result); $description = nl2br($row['description']); //start outputting the product info echo <<<EOF <div> <!-- left side --> <img src=$row['pic_url'] alt='' /><br /> <p>$description</p> </div> <div> <!-- right side --> <p>$row['name']</p> <p>$row['price']</p> </div> EOF;}$_SESSION['cartInfo'] = urlencode(serialize($myCart));ob_end_flush();?>[/code]If I've counted my lines correctly, it looks like it's breaking where I'm trying to output the image in the heredoc.Any ideas? Quote Link to comment Share on other sites More sharing options...
hitman6003 Posted December 12, 2006 Share Posted December 12, 2006 the "EOF;" should not be tabbed over...it should be the first and only thing on the line. Quote Link to comment Share on other sites More sharing options...
KevinM1 Posted December 12, 2006 Author Share Posted December 12, 2006 Removing the tab didn't help.... Quote Link to comment Share on other sites More sharing options...
hitman6003 Posted December 12, 2006 Share Posted December 12, 2006 Just eliminate the HEREDOC syntax then:[code] echo ' <div> <!-- left side --> <img src="' . $row['pic_url'] . '" alt="" /><br /> <p>' . $description . '</p> </div> <div> <!-- right side --> <p>' . $row['name'] . '</p> <p>' . $row['price'] . '</p> </div>';[/code] Quote Link to comment Share on other sites More sharing options...
KevinM1 Posted December 12, 2006 Author Share Posted December 12, 2006 It looks like my problem was some sort of double quote/single quote conflict. Putting everything into one big echo statement, while ugly, fixed the problem.I have another syntax problem in a different file, though. I'm trying to do a session test, checking to see if my sessions are saving the objects I've created properly. I get the following error:[quote]Fatal error: Call to a member function showCart() on a non-object in /home/thinkin8/public_html/sessiontest2.php on line 17[/quote]My class code is:[code]<?phpclass ShoppingCart{ private $Cart; public function addProduct($name, $price, $quantity){ $this -> Cart[] = new Product($name, $price, $quantity); } public function clearCart(){ $this -> Cart = array(); } public function getTotal(){ $total = 0; foreach($this->Cart as $value){ $total += $value -> getPrice(); } return $total; } public function showCart(){ echo "<table><tr><th>Name:</th><th>Quantity:</th><th>Total Price:</th></tr>\n"; foreach($this -> Cart as $value){ echo "<tr><td>{$value->getName()}</td><td>{$value->getQuan()}</td><td>\${$value->getPrice()}</td></tr>\n"; } echo "</table>\n"; }}?>[/code]I have one file that populates the cart, and another that outputs it, in order to see if my session info is being passed properly from page to page. The problem seems to be stemming from the testCart variable not realizing it's a ShoppingCart object. My setup code is:[code]<?php#sessiontest.phpinclude('../php_config/config.php');session_start();$testCart = new ShoppingCart();$testCart -> addProduct('Tomatoes', 0.99, 4);$testCart -> addProduct('Playstation 3', 600.00, 1);$testCart -> addProduct('Nike Shoes', 75.00, 1);$_SESSION['testCart'] = urlencode(serialize($testCart));?><html><head></head><body><a href="sessiontest2.php">Click</a></body></html>[/code]My code calling the showCart method is:[code]<?php#sessiontest2.phpinclude('../php_config/config.php');session_start();if(!isset($_SESSION['testCart'])){ $testCart = new ShoppingCart();}else{ $testCart = urldecode(unserialize($_SESSION['testCart']));}$testCart -> showCart();?>[/code]Any ideas on how I can get it to think it's a ShoppingCart object? Quote Link to comment Share on other sites More sharing options...
hitman6003 Posted December 12, 2006 Share Posted December 12, 2006 Reverse your urldecode / unserialize functions...[code]else{ $testCart = unserialize(urldecode($_SESSION['testCart']));}[/code]Also, urlencode shouldn't be necessary...the session data is stored on the server...only a session id is sent to the user (via a cookie when possible, or the url if cookies are disabled). Quote Link to comment Share on other sites More sharing options...
KevinM1 Posted December 12, 2006 Author Share Posted December 12, 2006 Sweet, that did the trick!Thanks for your help. :) Quote Link to comment Share on other sites More sharing options...
KevinM1 Posted December 14, 2006 Author Share Posted December 14, 2006 Well, I'm getting another OOP syntax error. It's: [quote]Warning: Invalid argument supplied for foreach() in /home/thinkin8/php_classes/ShoppingCart.php on line 27[/quote]The error appears when I try using the ShoppingCart's showCart method.Code using the class:[code]<?phpinclude('../php_config/config.php');session_start();ob_start();if(!isset($_SESSION['ip'])){ $ip = $_SERVER['REMOTE_ADDR']; $myCart = new ShoppingCart();}else{ if($_SERVER['REMOTE_ADDR'] != unserialize($_SESSION['ip'])){ $ip = $_SERVER['REMOTE_ADDR']; $myCart = new ShoppingCart(); } else{ $ip = unserialize($_SESSION['ip']); $myCart = unserialize($_SESSION['myCart']); }}include('../templates/header.inc');echo "IP address: $ip<br />\n";$myCart -> showCart();echo <<<EOT<div> <img src="images/store/storefront_01.jpg" alt="" /><img src="images/store/storefront_02.jpg" alt="" /></div><div> <a href="viewcat.php?cat=acer_laptops"><img src="images/store/storefront_05.jpg" alt="" /></a> <img src="images/store/storefront_06.jpg" alt="" /> <a href="viewcat.php?cat=lenovo_laptops"><img src="images/store/storefront_04.jpg" alt="" /></a></div><div> <a href="viewcat.php?cat=averatec_laptops"><img src="images/store/storefront_09.jpg" alt="" /></a> <a href="viewcat.php?cat=panasonic_laptops" style="margin-left: 68px;"><img src="images/store/storefront_11.jpg" alt="" /></a></div><div> <img src="images/store/storefrontbar.jpg" alt="" /></div><div> <a href="viewcat.php?cat=acer_desktops"><img src="images/store/storefront_05.jpg" alt="" /></a> <img src="images/store/desktop.jpg" style="margin-left: 25px; alt="" /> <a href="viewcat.php?cat=lenovo_desktops" style="margin-left: 30px;"><img src="images/store/storefront_04.jpg" alt="" /></a></div><div> <a href="#"><img src="images/store/storefront_19.jpg" alt="" /></a> <a href="viewcat.php?cat=dandh_desktops" style="margin-left: 53px;"><img src="images/store/storefront_21.jpg" alt="" /></a></div><div> <img src="images/store/storefrontbar.jpg" alt="" /></div><div> <a href="viewcat.php?cat=peripherals"><img src="images/store/storefront_24.jpg" alt="" /></a></div><div> <img src="images/store/storefront_25.jpg" alt="" /></div>EOT;include('../templates/footer.inc');$_SESSION['ip'] = serialize($ip);$_SESSION['myCart'] = serialize($myCart);ob_end_flush();?>[/code]My ShoppingCart class:[code]<?phpclass ShoppingCart{ private $Cart; public function addProduct($name, $price, $quantity){ $this -> Cart[] = new Product($name, $price, $quantity); } public function clearCart(){ $this -> Cart = array(); } public function getTotal(){ $total = 0; foreach($this->Cart as $value){ $total += $value -> getPrice(); } return $total; } public function showCart(){ echo "<table><tr><th>Name:</th><th>Quantity:</th><th>Total Price:</th></tr>\n"; foreach($this->Cart as $value){ echo "<tr><td>{$value->getName()}</td><td>{$value->getQuan()}</td><td>\${$value->getPrice()}</td></tr>\n"; } echo "</table>\n"; }}?>[/code]Interestingly, I had originally had my session variables both url(de/en)coded and (un)serialized, which gave me the error: [quote]Fatal error: Call to a member function showCart() on a non-object in /home/thinkin8/public_html/storefront.php on line 28[/quote]I'm thinking my error is of the 'duh' variety, but I can't seem to find it. Any ideas?Thanks :) Quote Link to comment Share on other sites More sharing options...
hitman6003 Posted December 14, 2006 Share Posted December 14, 2006 It's a warning, so it's not affecting the execution. Do you still get that warning if there are items in the shopping cart? Most likely the $Cart variable is empty, so there is nothing for it to loop through, which causes a warning from foreach. Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted December 15, 2006 Share Posted December 15, 2006 not sure if this is the problem, or if my coding has just been bad for a long time, but when you call a new class like:[code=php:0]$myCart = new ShoppingCart();[/code]i always thought that you didnt have to have the () after it... Quote Link to comment Share on other sites More sharing options...
KevinM1 Posted December 16, 2006 Author Share Posted December 16, 2006 [quote author=hitman6003 link=topic=118331.msg485052#msg485052 date=1166129945]It's a warning, so it's not affecting the execution. Do you still get that warning if there are items in the shopping cart? Most likely the $Cart variable is empty, so there is nothing for it to loop through, which causes a warning from foreach.[/quote]Sorry for the delay in response.I've tried the showCart method with an empty cart before, and it executed properly (showing an empty cart) without any warnings. I think the problem is with me trying to save/restore cart info from the session data. Quote Link to comment Share on other sites More sharing options...
KevinM1 Posted December 17, 2006 Author Share Posted December 17, 2006 I hate to bump this up, but I still haven't found the cause of the error, and I'd like to fix it before my site goes live. Please help. Quote Link to comment 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.