Page 1 of 1

2D Array help when posting from a form

Posted: Mon Feb 04, 2008 4:50 am
by crampo
~pickle | please use [ code=php] tags where appropriate. Your post has been updated to reflect how we'd like it to look.
Hi, I'm struggling with a problem on a form where i'm allowing the user to update multiple records at the one time. My mind has gone completely blank as to how i retrieve the form data correctly.

the form code is as follows:

Code: Select all

//counter
$i = 0;
while ($row = $connector->fetchArray($result)){
                        
    $id = $row['id'];
    echo "<tr>";
    echo "<td><input type='text' name='date[$i]' class='date' value='".$row['date']."'</td>
    <td><input type='text' name='team[$i]  class='team' value='".$row['team']."'</td>
    <td><input type='text' name='opposition[$i]'  class='opposition' value='".$row['opposition']."'</td>
    <td><input type='text' name='venue[$i]' class='venue' value='".$row['venue']."'</td>
    <td><input type='text' name='kick_off[$i]' class='kick_off' value='".$row['kick_off']."'</td>
    <td><input type='text' name='arrangements[$i]' class='arrangements' value='".$row['arrangements']."'</td>
    </td><td><a href='delete_fixture.php?delete_id=".$row['id']."'>Delete</a></td>";
    echo "<input type='hidden' name='id[$i]' value='$row[id]' />";
    echo "</tr>";
    $i++;
}       
    echo "</table>";
    echo "<input type='submit' class='submit' name='update_this_weeks' value='Update'/>";
the problem is how i actually retrieve each individual record's values, at the moment i'm not managing to do this properly...

Code: Select all

//if fixtures have been updated
if (isset($_POST['update_this_weeks'])) {
 
echo '<pre>'; print_r($_POST); //for debugging
 
//trying (and failing) to extract data for each record
$count = count($_POST);
for ($i=0; $i< $count; $i++)
{
    $id = (int) $_POST['id'][$i];
    $date= mysql_real_escape_string($_POST['date'][$i]);
    $team= mysql_real_escape_string($_POST['team'][$i]);
    echo $date; //just prints "Array"
} 
die;
}
The output of the print_r($_POST) is as follows:

Code: Select all

Array
(
[drop_option] => fixture_edit.php?sport_id=1
[drop_sub_option] => fixture_edit.php?sport_id=1&team_id=19
[date] => Array
[team] => Array
[opposition] => Array
[venue] => Array
[kick_off] => Array
[arrangements] => Array
[id] => Array
[update_this_weeks] => Update
)
If anyone can help that would be great as i'm pretty stuck right now!
~pickle | please use [ code="php" ] tags where appropriate. Your post has been updated to reflect how we'd like it to look.

Re: 2D Array help when posting from a form

Posted: Mon Feb 04, 2008 7:28 am
by Kieran Huggins
try something like:

Code: Select all

<input type='text' name='game[$i][team]'  class='team' value='".$row['team']."'/>
<input type='text' name='game[$i][opposition]'  class='opposition' value='".$row['opposition']."'/>
<input type='text' name='game[$i][venue]' class='venue' value='".$row['venue']."'/>

Code: Select all

foreach($_POST['game'] as $game){
  echo $game['team']." plays ".$game['opposition']." at ".$game['venue'];
}