Jump to content

Recommended Posts

Yes, this is the same one as before, but I figure I might get a response if I put all of the info in one post.

I'm getting the following warning: [quote]Warning: Invalid argument supplied for foreach() in /home/thinkin8/php_classes/ShoppingCart.php on line 27[/quote]

This is the code utilizing the ShoppingCart class (it's autoloaded through the included config.php):
[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(urldecode($_SESSION['ip']))){
      $ip = $_SERVER['REMOTE_ADDR'];
      $myCart = new ShoppingCart();
  }

  else{
      $ip = unserialize(urldecode($_SESSION['ip']));
      $myCart = unserialize(urldecode($_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'] = urlencode(serialize($ip));
$_SESSION['myCart'] = urlencode(serialize($myCart));

ob_end_flush();

?>
[/code]

This is the ShoppingCart class itself.  As you can see, line 27 is the foreach of the showCart method
[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]

Another poster suggested that having a $myCart variable with an empty $Cart property was causing the warning.  This is not the case, however, as my test script, with an empty cart, generates no warnings whatsoever.  My test code:
[code]
<?php

#test3.php -- test to see if the shopping cart works

include('../php_config/config.php');

$cart = new ShoppingCart();

$cart -> addProduct('tomato', 0.99, 5);
$cart -> addProduct('Nike Shoes', 50.99, 1);
$cart -> addProduct('Playstation 3', 600.00, 1);

$cart -> showCart();

$total = $cart -> getTotal();

echo "<br />\nTotal price of the items in your shopping cart: \${$total}<br />\n";

$cart -> clearCart();

$cart -> showCart();

$total = $cart -> getTotal();

echo "<br />\nTotal price of the items in your shopping cart: \${$total}<br />\n";

?>
[/code]

In this script, showCart works flawlessly, even after clearCart is called.  No warnings at all.  What makes this so frustrating is that both scripts (my test script and my live script) are on the same server, so they're both running the same version of PHP (5.1.6) and they're both using the same ShoppingCart file, as there's only one on the server.

So, is this a case of PHP acting buggy?  Or is there anything else I can try?

Thanks.
Link to comment
https://forums.phpfreaks.com/topic/31091-solved-frustrating-syntax-warning/
Share on other sites

it could be a case of your error_reporting level as to why you're getting the error on one and not the other.
you could always do a check just before the 'foreach' such as:

[code]
<?php
if (sizeof($this->Cart) == 0)
{
  return;
}
?>
[/code]

the invalid argument would be a result of the foreach being passed something other than an array (including a null value). you can also try changing the $Cart declaration in your class to:

[code]
<?php
  private $Cart = array();
?>
[/code]
[quote author=redbullmarky link=topic=119095.msg487249#msg487249 date=1166446619]
the invalid argument would be a result of the foreach being passed something other than an array (including a null value). you can also try changing the $Cart declaration in your class to:

[code]
<?php
   private $Cart = array();
?>
[/code]
[/quote]

That did the trick!  Thanks a lot! :)
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.