Jump to content

Recommended Posts

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 script

include('../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?
Link to comment
https://forums.phpfreaks.com/topic/30374-syntax-error/
Share on other sites

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]
Link to comment
https://forums.phpfreaks.com/topic/30374-syntax-error/#findComment-139759
Share on other sites

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]
<?php

class 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.php

include('../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.php

include('../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?
Link to comment
https://forums.phpfreaks.com/topic/30374-syntax-error/#findComment-139774
Share on other sites

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).
Link to comment
https://forums.phpfreaks.com/topic/30374-syntax-error/#findComment-139784
Share on other sites

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]
<?php

include('../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]
<?php

class 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 :)
Link to comment
https://forums.phpfreaks.com/topic/30374-syntax-error/#findComment-141298
Share on other sites

[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.
Link to comment
https://forums.phpfreaks.com/topic/30374-syntax-error/#findComment-142654
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.