Results 1 to 4 of 4

Thread: foreach() ??

  1. #1
    Join Date
    May 2010
    Location
    Sacramento, CA
    Posts
    91
    Thanks
    23
    Thanked 2 Times in 2 Posts

    Default foreach() ??

    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

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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:
    Code:
    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'].
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    May 2010
    Location
    Sacramento, CA
    Posts
    91
    Thanks
    23
    Thanked 2 Times in 2 Posts

    Default

    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:

    Code:
    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!

  4. #4
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •