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.
Invalid argument supplied for foreach() loop
Page 1 of 13 Replies - 242 Views - Last Post: 18 September 2012 - 05:19 AM
Replies To: Invalid argument supplied for foreach() loop
#2
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?
#3
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
Then you use
Hope it helps you.
Cheers
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
#4
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:
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.
However, I'd suggest just doing:
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.
}
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote





|