Solved

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

Posted on 2007-11-13
5
381 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 50 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 50 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

867 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now