Jump to content


Photo

convert post variables to array


  • Please log in to reply
2 replies to this topic

#1 ultimatemonty

ultimatemonty
  • Members
  • Pip
  • Newbie
  • 4 posts

Posted 20 July 2006 - 06:20 PM

***posted this in PHP General, then realized here was a better place....sorry for the dupe...***

I'm working on a stat tracking application for my ultimate frisbee team. The 'Add Stats' form is generated dynamically based on what tournament you are working on, what game you are adding stats for, and which players attended that tournament.  Each form field has a common name (player, tgs, lgs, sgs, etc etc) followed by the players ID number to identify those stats as belonging to a specific player. so for player ID 3, the form fields would be player_3, tgs_3, sgs_3, etc etc).  There are 8 data fields per player, and there can be upwards of 20 players submitted in a form.

Instead of creating individual $_POST('....'] variables for each submitted value (which could be very tedious), is there a way to store the values for each player in a multi-dimensional array, then have the SQL Data Insertion code loop through each value in the array? I haven't done much with arrays in my programming, so I'm not real sure how to work with them.

Thanks so much!
--Monty

#2 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 20 July 2006 - 07:16 PM

Use a foreach loop to automatically create the various variables:
foreach($_POST as $field => $value)
{
   //automatically create the various variables:
   $$field = mysql_real_eascape_string($value));
}
Now say you have forum fields called player you use $player to access that fields values, rather than $_POST['player']

#3 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,016 posts

Posted 20 July 2006 - 07:57 PM

Instead of appending the id to the field name, use id as an array index. EG

<form>
       <input type="text" name="player[1]">
       <input type="text" name="tgs[1]">
       <input type="text" name="lgs[1]">
       <input type="text" name="sgs[1]">
       
       
       <input type="text" name="player[2]">
       <input type="text" name="tgs[2]">
       <input type="text" name="lgs[2]">
       <input type="text" name="sgs[2]">
       
       
       <input type="text" name="player[3]">
       <input type="text" name="tgs[3]">
       <input type="text" name="lgs[3]">
       <input type="text" name="sgs[3]">
       
       <input type="submit" name="submit" value="submit">
</form>

To process

foreach ($_POST['player'] as $id => $player) {
        $tgs = $_POST['tgs'][$id];
        $lgs = $_POST['lgs'][$id];
        $sgs = $_POST['sgs'][$id];
        // process player data
}

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users