3 Replies - 403 Views - Last Post: 18 September 2012 - 05:19 AM Rate Topic: -----

#1 Travis1012  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 36
  • View blog
  • Posts: 222
  • Joined: 05-August 09

Invalid argument supplied for foreach() loop

Posted 17 September 2012 - 05:00 PM

I have been trying to work out why there is a problem with this loop for a while now. I have tried a few variations but I thought maybe some help from other programmers might help.

$cartItems = unserialize (base64_decode ($_SESSION['cartItems']));
foreach ($cartItems as $productId => $products)
{

// Other code in here.

}

This is from my clients website, I did not code the site but have been asked to fix the site up.

Do you have any suggestions? it would be greatly appreciated.

Thank you in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: Invalid argument supplied for foreach() loop

#2 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2990
  • Posts: 10,332
  • Joined: 08-August 08

Re: Invalid argument supplied for foreach() loop

Posted 17 September 2012 - 06:18 PM

  • Is $_SESSION['cartItems'] empty?
  • If it's not empty, was it serialized and then encoded or encoded and then serialized?

Was This Post Helpful? 0
  • +
  • -

#3 ludo237  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: -1
  • View blog
  • Posts: 21
  • Joined: 04-September 10

Re: Invalid argument supplied for foreach() loop

Posted 18 September 2012 - 01:04 AM

I think that the $_SESSION[] variable is empty.
You should always check if is isset and not empty
 (isset($_SESSION['cartItems']) && !empty($_SESSION['cartItems']) ? $check = true : $check = false; 

Then you use
 $check 

if($check == true){
$cartItems = unserialize (base64_decode ($_SESSION['cartItems']));
foreach ($cartItems as $productId => $products)
{

// Other code in here.

}



Hope it helps you.
Cheers

This post has been edited by ludo237: 18 September 2012 - 01:05 AM

Was This Post Helpful? -1
  • +
  • -

#4 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3719
  • View blog
  • Posts: 5,990
  • Joined: 08-June 10

Re: Invalid argument supplied for foreach() loop

Posted 18 September 2012 - 05:19 AM

@ludo237
There is no need to call both isset and empty. The empty function includes an isset check, and like the isset function it will not trigger a notice when the value is not set.

I'd also question the need to use the ternary syntax and assigning the check to a variable. Both just complicate the syntax, in my opinion. (Although, I suppose some degree of personal preference is involved in this.) Your use of the ternary syntax is a bit unusual, as well. Most would probably do:
$check = isset($var) ? true : false;


Or, better yet, just leave out the ternary operator and let the results stand. There is no need to explicitly assign true or false based on a Boolean check. The check itself is value enough.
$check = isset($var);



However, I'd suggest just doing:
if (!empty($_SESSION["cartItem"])) {
    // Use the session value here.
}


Was This Post Helpful? 4
  • +
  • -

Page 1 of 1