0

Please point me in the right direction here Im trying to do the following:

  1. Multiple select boxes are generated for each eventid
  2. User choose who they think will win each event
  3. The name of each selectBox is the event id assigned to variable $id
  4. At end of while loop I want to extract the array $id value in For loop, however im getting error "undefined offset & invalid argument" on my for loop...

Here is my form

enter image description here

    $i=0;//counter
    while($row=mysql_fetch_array($result)){

    $team1 = $row['team1'];
    $team2 = $row['team2'];
    $id[$i]= $row['event_id']; 

    echo'<h3>'.$team1.' VS '.$team2.'</h3>';
    echo'<select name="'.$id[$i].'">';
            echo'<option value="'.$row['team1'].'">'.$team1.'</option>';
            echo'<option value="'.$row['team2'].'">'.$team2.'</option>';
            echo'</select>';    
        $i++;
    }//while

Here is my for loop giving error, I suspect problem is in the $_POST['$id']...

if(isset($_POST['submit'])){    

    foreach($_POST[$id] as $eventId => $winner){
     echo'<h3>'.$eventId.'</h3>';
}//for loop
}//end isset

Any help will be greatly appreciated

Marilee
  • 1,598
  • 4
  • 22
  • 52
  • where id `$id` is defined and how it is initialized? – Iłya Bursov Apr 17 '15 at 17:58
  • @Lashane is this what you are asking $id[$i]= $row['event_id']; WHERE $row['event_id']; is retrieved from database – Marilee Apr 17 '15 at 17:59
  • possible duplicate of [PHP: "Notice: Undefined variable" and "Notice: Undefined index"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – chris85 Apr 17 '15 at 18:00
  • What is the exact error message and to what line does it relate? – ficuscr Apr 17 '15 at 18:01
  • 1
    I think you need to change $id to just id in your code. You are using a variable when you are actually trying to use a constant. – Maximus2012 Apr 17 '15 at 18:01
  • no, before `isset` you have to define scalar (not array) variable `$id` - how and where it is defined? note - this variable has nothing to do with $id array which is used in first part of your code – Iłya Bursov Apr 17 '15 at 18:02
  • @ficuscr the message is Illegal offset type and Invalid argument supplied for foreach() – Marilee Apr 17 '15 at 18:04
  • Then thinking its as Maximus2012 said then... see: http://stackoverflow.com/questions/6628/php-array-indexing-arrayindex-vs-arrayindex-vs-arrayindex – ficuscr Apr 17 '15 at 18:08
  • @Maximus2012 that works but returns the index of the array and does not give me the event_id im looking for – Marilee Apr 17 '15 at 18:08
  • @Marilee does the answer from Digits below help ? – Maximus2012 Apr 17 '15 at 18:13
  • @Maximus2012 its pointing me in the correct directions im playing around with the idea but doesn't quite work – Marilee Apr 17 '15 at 18:18
  • What part is not working? On a different note, could you use radio buttons in place of dropdown list since your logic needs to choose one of the two options. – Maximus2012 Apr 17 '15 at 18:19
  • @Maximus2012 I got it working thank you so much for the help, ill change to radiobuttons ;-) – Marilee Apr 17 '15 at 18:28

2 Answers2

2

$id is defined as an array here$id[$i]= $row['event_id'];. Arrays can not be used as a key in a foreach array or otherwise. This is what is causing you an error on this line

foreach($_POST[$id] as $eventId => $winner){ //$id is an array of values
    echo'<h3>'.$eventId.'</h3>';
}//for loop

You have to make a second foreach statement for the $id then use the id values in your current foreach statement.

foreach( $id as $key => $val ) {
foreach( $_POST[$val] as $eventId => $winner){
Digits
  • 2,634
  • 2
  • 14
  • 23
0

You don't know what the event_id values are on the action page, so I think you'll have to query for those again something like:

if(isset($_POST['submit'])){
    /* do your query here */
    $data = array();
    while($row=mysql_fetch_array($result)){
     $data[] = $_POST[$row['event_id']];
    }

    foreach($data as $key => $value){
        print_r($_POST[$value]);
        echo "<br>";
    }
}
earl
  • 36
  • 5