Solved

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

Posted on 2007-11-13
5
380 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks to both of you for your help!
David
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
This article discusses four methods for overlaying images in a container on a web page
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 create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

743 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

17 Experts available now in Live!

Get 1:1 Help Now