Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old February 13th, 2004, 09:40 AM
Registered User
 
Join Date: Feb 2004
Location: New Delhi, Delhi, India.
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Invalid argument supplied for foreach()

Hi,
I am new to php 4 days only. My prior programming experience includes C,C++,VB6,VC6(MFC,ATL & SDK), ASP & the scripting languages VBScript & JavaScript.
I had started with Professional PHP4 (Wrox) which refers to ver. 4.0.5 and since I have been using php 4.3.4 which now has register_globals "Off" by default.I am facing a small yet very annoying problem.
If I were To access "pref_cities[]" from This code faragment. {Ref. Ch. 07 - job.php The Complex Forms example.}

<select name="pref_cities[]" multiple size="3">
            <option>Nagpur</option>
            <option>Mumbai</option>
            <option>Bangalore</option>
            <option>Chennai</option>
            <option>Kolkatta</option>
</select>


Why am I getting Invalid Argument Supplied for foreach() When I try to use the following :


foreach ($_POST['pref_cities'] as $cities)
{
echo "You are Willing To Work in - $cities <br>";
}

A workaround would be to turn register_globals "On". But that is not the solution.
Help Please.

Look Past The Flesh
AND
See Your Enemy!
Reply With Quote
  #2 (permalink)  
Old February 13th, 2004, 01:41 PM
Friend of Wrox
Points: 2,570, Level: 21
Points: 2,570, Level: 21 Points: 2,570, Level: 21 Points: 2,570, Level: 21
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: San Diego, CA, USA
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Well, the code you have looks totally reasonable to me.

foreach() is a looping construct that iterates across each item in the array and sets variables containing the index and value for each iteration.

For example, if you had an array:

$cities = array("Mumbai", "Kolkatta");

foreach($cities as $city)
{
    echo "You'd work in $city.\n";
}

would print "You'd work in Mumbai.\n" in the first iteration and "You'd work in Kolkatta.\n" on the second.
  http://www.php.net/foreach

As such, your code looks fine -- $_POST['pref_cities'] is an array, just like $cities is in my little example above.

The only thing I can think of is that you're getting this error before the user submits the form. If $_POST doesn't exist, then you'll get an "undefined index" warning telling you that the $_POST array doesn't contain an index named "pref_cities".


The other thing I should talk about is what the empty brackets ("[]") mean. PHP uses this notation to append items to the end of an array. Empty brackets will create a new numeric index where the index is equal to 1 + <the greatest existing numerical index>.

In your HTML form, you have an input who's name is "pref_cities[]". If you look at the HTTP POST header information, you'll see that each city in a multiple selection is set as a distinct variable. That gets interpreted by PHP as individual assignments. The PHP version of this would be:

  $pref_cities[] = <city 1>;
  $pref_cities[] = <city 2>;
            ...
  $pref_cities[] = <city n>;

So you see, this continually appends each city to the end of the $pref_cities array.

That's important to know -- the name of the array variable is "pref_cities", _NOT_ "pref_cities[]".


Hope this helps clear things up!



Take care,

Nik
http://www.bigaction.org/
Reply With Quote
  #3 (permalink)  
Old February 14th, 2004, 07:05 AM
Registered User
 
Join Date: Feb 2004
Location: New Delhi, Delhi, India.
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Perhaps I should have been more clear. I know it had to warn me about "undefined index" warning as it will do for any for any form element before it is submitted. But my question is why am I getting an "Invalid argument..." instead of an "undefined index" warning??

Look Past The Flesh
AND
See Your Enemy!
Reply With Quote
  #4 (permalink)  
Old February 14th, 2004, 01:56 PM
Friend of Wrox
Points: 2,570, Level: 21
Points: 2,570, Level: 21 Points: 2,570, Level: 21 Points: 2,570, Level: 21
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: San Diego, CA, USA
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Sort of -- it will only give you the undefined index warning if you are reporting E_NOTICE level errors. Check error_reporting in php.ini -- if it's E_ALL, then you should see warnings. If it's E_ALL & ~E_NOTICE, you won't.

You'll get the invalid argument error when you're passing a non-array variable to foreach.

It's situations like this where debug functions like print_r() come in handy:

echo "<pre>";
print_r($_POST);
echo "</pre>\n"

That will show you what's actually in $_POST, and you can determine why your index isn't an array.


Take care,

Nik
http://www.bigaction.org/
Reply With Quote
  #5 (permalink)  
Old September 24th, 2010, 03:40 AM
Registered User
 
Join Date: Sep 2010
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

function get() {
$result = mysql_query("SELECT * FROM user where id='2'");
echo $result."<br>";
while($row = mysql_fetch_array($result)) {
echo $row['id'];
echo '--';
echo $row['email'];
echo '--';
echo $row['name'];
echo '<br>';
}
if (!mysql_num_rows($result)>0) {
echo 'No record found!';
} else {
foreach($result as $data) {
echo $data['name'];
//echo $data->name;
}
}
}

get();


my browser shows:
Resource id #3
2--hello@a.com--itsmeka

Warning: Invalid argument supplied for foreach() in \htdocs\1-2\includes\db.php on line 91
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Invalid Argument ppenn Access VBA 2 May 14th, 2007 10:11 AM
Invalid postback or callback argument. kingw BOOK: Professional Ajax ISBN: 978-0-471-77778-6 1 January 1st, 2007 08:49 PM
mailling list:mysql_num_rows(): supplied argument jamesz20 Beginning PHP 2 December 6th, 2006 10:45 AM
fwrite(): supplied argument is not a valid stream Adam H-W Beginning PHP 3 January 20th, 2006 01:09 AM
Invalid Argument.... baNTON Access VBA 4 April 27th, 2004 06:18 AM



All times are GMT -4. The time now is 11:43 AM.


Powered by vBulletin®
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.