Solved

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

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

Technology Partners: 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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

733 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