coleman Posted December 3, 2009 Share Posted December 3, 2009 Hi all, I am new to this forum... It has been awhile since i have worked with arrays, and i am starting to pull my hair out with this little issue i am having.. I hope someone can help me out with it, or even possibly show me a better way of doing this. I am creating an web based hockey stats program for a ball hockey league. I have a score sheet where they enter data. The score sheet gets constructed in a single form with a row for each player on the team. there are 12 players on each team so there will be 24 rows of data in the end. I need to store each players game stats individually in a stats table where i can keep track of player game stats for each game. here is the code for my form. while ($r_hmp = mysql_fetch_array($q_hm_play)) { echo "<tr>"; echo "<td>".$r_hmp['jnum'] ."</td>"; echo "<td>".$r_hmp['lname'].", ".$r_hmp['fname']." ( ".$r_hmp['position'] ." )<input type='hidden' name='pid[]' value='".$r_hmp['id']."'></td>\n"; echo "<td><input type='text' name='gp[]' size='2' maxlength='2'></td>\n"; echo "<td><input type='text' name='g[]' size='2' maxlength='2'></td>\n"; echo "<td><input type='text' name='a[]' size='2' maxlength='2'></td>\n"; echo "<td><input type='text' name='pns[]' size='2' maxlength='2'></td>\n"; echo "<td><input type='text' name='pim[]' size='2' maxlength='2'></td>\n"; echo "<td><input type='text' name='gw[]' size='2' maxlength='2'></td>\n"; echo "<td><input type='text' name='pg[]' size='2' maxlength='2'></td>\n"; echo "<td><input type='text' name='sg[]' size='2' maxlength='2'></td>\n"; echo "<td><input type='text' name='pa[]' size='2' maxlength='2'></td>\n"; echo "<td><input type='text' name='sa[]' size='2' maxlength='2'></td>\n"; echo "<td><input type='text' name='soa[]' size='2' maxlength='2'></td>\n"; echo "<td><input type='text' name='sog[]' size='2' maxlength='2'></td>\n"; echo "<td><input type='text' name='psa[]' size='2' maxlength='2'></td>\n"; echo "<td><input type='text' name='psg[]' size='2' maxlength='2'></td>\n"; echo "<td><input type='text' name='mj[]' size='2' maxlength='2'></td>\n"; echo "<td><input type='text' name='mc[]' size='2' maxlength='2'></td>\n"; echo "<td><input type='text' name='gm[]' size='2' maxlength='2'></td>\n"; echo "<td><input type='text' name='gr[]' size='2' maxlength='2'></td>\n"; echo "<td><input type='text' name='mt[]' size='2' maxlength='2'></td>\n"; echo "</tr>\n"; $hi++; one the score keeper clicks the save button i am trying to create a multi-leveld array where i store 24 rows of data each row should hold the players id and his particular stats for the game. i tried to setup my array so i could loop troug it using two counters $player[$i][$x] where i would be the count of the row and x being the stat data. here is the code i have tried, and to no avail.... your help with this would be greatly appreciated. I am sure there is a simple solution to this, but i am stuck and can't seem to find any info that might point me in the right direction. thanks in advance $hmpc = count($_POST['pid']); $i = 0; while ($i <= $hmpc) { $player = array($_POST['pid'],array($_post['gp']); $i++; } $i = 0; $x=0; echo $hmpc; while ($i < $hmpc) { while ($x < $hmpc) { echo 'Player Data: '.$player[$i][$x]; $x++; } $i++; Quote Link to comment https://forums.phpfreaks.com/topic/183805-help-with-form-post-data-and-arrays/ Share on other sites More sharing options...
coleman Posted December 3, 2009 Author Share Posted December 3, 2009 Well i think i found a way around my problem. I don't think its efficient in any shape or form but it works I made separate arrays for each form element which is posted as array data.. i then used a counter to walk through each array at the same level so all the pointers stay in line.. I will continue on with this method until i can find another more efficient method of coding this.. any help would be appreciated. See above form generation code for how the form is built. here is my new working method... $hmpc = count($_POST['pid']); // player data arrays $hpid = array(); //create array $hgp = array(); //game played $hg = array(); // goals $ha = array(); // assists $hpns = array(); $hpim = array(); $hgw = array(); $hpg = array(); $hsg = array(); $hpa = array(); $hsa = array(); $hsoa = array(); $hsog = array(); $hpsa = array(); $hpsg = array(); $hmj = array(); $hmc = array(); $hgm = array(); $hgr = array(); $hmt = array(); // add post data to respective arrays $hpid = $_POST['pid']; $hgp = $_POST['gp']; $hg = $_POST['g']; $ha = $_POST['a']; $i =0; while ($i < $hmpc) { echo "Player id = " . $hpid[$i] ."<br>"; echo "Game Played = " .$hgp[$i] ."<br>"; echo "Goals = " .$hg[$i] ."<br>"; $i++; } Quote Link to comment https://forums.phpfreaks.com/topic/183805-help-with-form-post-data-and-arrays/#findComment-970240 Share on other sites More sharing options...
PFMaBiSmAd Posted December 3, 2009 Share Posted December 3, 2009 The $_POST[...] variables are already arrays. There is no need to define more arrays, copy each $_POST array into them, then loop through the arrays. Just loop through the $_POST arrays directly. See the 'form processing code" at this link - http://www.phpfreaks.com/forums/index.php/topic,278815.msg1320035.html#msg1320035 (note: the indexes in the code at that link start at 1, not 0 as you are using, so if you use any of the code at that link, adjust it to match your array index values.) Quote Link to comment https://forums.phpfreaks.com/topic/183805-help-with-form-post-data-and-arrays/#findComment-970252 Share on other sites More sharing options...
coleman Posted December 3, 2009 Author Share Posted December 3, 2009 ok thanks for the tip / help with arrays.. it's been awhile since i've worked with them. Would there be a more efficient way to complete what i am trying to do or would this be the best method( minus recreating the arrrays)? Quote Link to comment https://forums.phpfreaks.com/topic/183805-help-with-form-post-data-and-arrays/#findComment-970438 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.