?
Solved

I'm getting an error on ... foreach (@$_SESSION['cart'] as $id => $count") {

Posted on 2007-11-13
5
Medium Priority
?
391 Views
Last Modified: 2012-05-05
I'm getting an error on ...
foreach (@$_SESSION['cart'] as $id => $count") {
The error reporting says:
Invalid argument supplied for foreach()....

I think it's the $id

What is $id?  I see it used in foreach() sometimes, but it seems to be crashing my code (and my book conveniently doesn't discuss it)

Can you please explain how the foreach() works?  Do I need to initialize $id?  As a Session variable?

Thanks,
David
0
Comment
Question by:DavidWare
  • 2
  • 2
5 Comments
 

Accepted Solution

by:
nmnrdn earned 200 total points
ID: 20277804
This error happens  when foreach argument is not declared as array();

 is_array($_SESSION['cart'])   should be true value.

0
 
LVL 3

Assisted Solution

by:samalraj
samalraj earned 200 total points
ID: 20277806
Hi David,
                 foreach has two forms as follows
foreach (array_expression as $value)
    statement
foreach (array_expression as $key => $value)
    statement

The first form loops over the array given by array_expression. On each loop, the value of the current element is assigned to $value and the internal array pointer is advanced by one (so on the next loop, you'll be looking at the next element).

The second form does the same thing, except that the current element's key will be assigned to the variable $key on each loop.

example for the second form:
<?php
$arr = array("one", "two", "three");
foreach ($arr as $key => $value) {
    echo "Key: $key; Value: $value<br />\n";
}
?>
in your case the $id is the key of the array so you dont need to initialize it

Regards,
Amal

0
 
LVL 3

Expert Comment

by:samalraj
ID: 20277822
update:
if the $_SESSION['cart'] is not an array or there is no such variable 'cart' in SESSION you will get
Warning: Invalid argument supplied for foreach()
I have modified your code to demonstrate it
<?php
$_SESSION['cart']=array("one", "two", "three");
?>
<?php
foreach (@$_SESSION['cart'] as $id => $count) {
      echo "Key: $id; Value: $count<br />\n";
}
?>
0
 

Author Comment

by:DavidWare
ID: 20277845
Oh, ok.  cart is the items being purchased.
So I have to initialize that Session as an array, and $id will work.
0
 

Author Comment

by:DavidWare
ID: 20277849
Thanks to both of you for your help!
David
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses
Course of the Month14 days, 18 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question