SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    a fresh, new start... dujmovicv's Avatar
    Join Date
    Aug 2006
    Location
    Earth
    Posts
    559
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    foreach line causing warning

    Hi All,

    I have this part of script which is 'getting' values via a form $_POST (in a foreach loop) :
    PHP Code:

    foreach($_POST['user'] as $key => $username) {
        foreach(
    $_POST['add_user_to_task'][$key] as $check => $checked){
            
    $task_name $_POST['task_name'][$key][$check];
            if (
    $checked == 1) {
                
            } 
    // end if CHECKED == 1
            
    else {
            }
        } 
    // end foreach 2
    // end foreach 1 
    Everything is fine, except when the user doesn't check ANY checkbox. Then I get the warning
    Code:
    Warning: Invalid argument supplied for foreach() in /path_to_file/ajax/ajaxupload.php on line 14
    which is just logic, cause the loop doesn't get the value for the key (it was NOT checked by the user)
    line 14 :
    PHP Code:
    foreach($_POST['add_user_to_task'][$key] as $check => $checked
    I can turn off warning messages by I really want the script to be clean. Now how can I 'tell' the second foreach NOT to run if there's no checked add_user_to_task[$i] value? Any help is appreciated! Regards

    Full time ADMIN - art community
    Part time coder - dsign

  2. #2
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,788
    Mentioned
    151 Post(s)
    Tagged
    3 Thread(s)
    PHP Code:
    if(isset($_POST['add_user_to_task'])) {
    // run it

    should do it
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  3. #3
    a fresh, new start... dujmovicv's Avatar
    Join Date
    Aug 2006
    Location
    Earth
    Posts
    559
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So simple... THNX

    Full time ADMIN - art community
    Part time coder - dsign

  4. #4
    a fresh, new start... dujmovicv's Avatar
    Join Date
    Aug 2006
    Location
    Earth
    Posts
    559
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmmmmm..... But, what if @ the first 'add_user_to_task' is checked (isset($_POST['add_user_to_task'] is TRUE)) and is NOT checked @ the next one? I'm still getting the warning...

    Full time ADMIN - art community
    Part time coder - dsign

  5. #5
    SitePoint Member
    Join Date
    Nov 2007
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Did you copy spikeZ code exactly? I think you'll need to check if the individual $key is set in that array so, just the same as his snippet but...

    PHP Code:
    if (isset($_POST['add_user_to_task'][$key])) {
    // run it


  6. #6
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,788
    Mentioned
    151 Post(s)
    Tagged
    3 Thread(s)
    ^ yup that would work nicely too.
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  7. #7
    a fresh, new start... dujmovicv's Avatar
    Join Date
    Aug 2006
    Location
    Earth
    Posts
    559
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you guys! I believe I'll manage to clear those warnings!

    Full time ADMIN - art community
    Part time coder - dsign


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
  •