?
Solved

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

Posted on 2007-11-13
5
Medium Priority
?
388 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

752 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