View Full Version : foreach() ??
crobinson42
05-13-2010, 01:04 AM
I am having a problem with foreach()
i have a form on a page that has check boxes, method="post" ---->
here is the code on the page the form links to:
echo $_POST['patrol'];
foreach($_POST['patrol'] as $patrol){
echo $patrol;
}
Now i'm not sure why but in the echo statement, it will show the info, but for the foreach() it says:
Warning: Invalid argument supplied for foreach() line 64
line 64 is --> foreach($_POST['patrol'] as $patrol){
is there something wrong with my code, hosting, or MY HEAD?!
Thanks
djr33
05-13-2010, 01:22 AM
Warning: Invalid argument supplied for foreach() line 64 Foreach can only take arrays. If only one item is sent, it will be sent as a single [string or other] variable, not as an array.
This is the most common reason for the error. Since I'm not entirely positive about what the variable is at that point (only testing live would show this), this IS just a guess, but I believe it will fix it.
In other words, you will get two types on input and you must use a foreach loop for them.
Here's a one line piece of code to add that will fix that:
Place this immediately before the foreach loop:
if (!is_array($_POST['patrol'])) { $_POST['patrol'] = array($_POST['patrol']); }
Some notes:
1. I don't know if it will ever actually be an array, but this will fix the foreach loop. Only in rare cases will you actually get an array sent from a form-- it's possible, though. I'd suggest reading up on this unless you're sure, so you know what to expect.
2. It's much better to not use $_POST or $_GET variables directly within your script. It's better to rename them to a regular variable before modifying them (so my code is messy in this sense). So at the start use $x = $_POST['x']; and work from there, not from $_POST['x'].
crobinson42
05-13-2010, 04:10 AM
sweet, thanks i looked into the array's coming from a form and it says to put a value into an array you do something like this:
echo '<input type="checkbox" value="'.$id.'" name="patrol[]">
The square brackets in the name="" put the value into an array now...
Thanks for the help!
djr33
05-13-2010, 04:58 AM
Correct. But unless you have a lot of those (and you might, but this is still overall quite rare), then this is not needed.
And I'm not sure if the incoming value will always be in an array if only one item was sent. You'd have to test this. The if statement I wrote above will handle it if that's the case.
Hopefully the brackets will force it into an array even with one item, but it might not (even depending on the browser...).
By the way, the one last problem you'll have with this is no value, such as if the form was not sent and you visit the page anyway. You will want to have a default value for each variable in case $_POST['x] does not exist.
The typical way to do that is this syntax:
$x = isset($_POST['x'])?$_POST['x']:'';
($x = [CONDITION]?[IF TRUE]:[ELSE]; )
The empty quotes at the end will setup a blank string variable. If you want an array, use array() (empty array) instead.
This will make the foreach loop work (exactly 0 times) and not trigger an incorrect type error.
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.