ninedoors Posted August 4, 2007 Share Posted August 4, 2007 I am using this code for to udpate a database that holds stats for my hockey league. This particular section updates the penalty minutes for a given players. I get these values by inputting them into a form and then using a script that updates the database. The penalty section is the difficult one for me because I in one textarea I input the players jersey number and then in onother textarea I input the corresponding penalty minutes for that player. So I get the values from the form and then convert it them to arrays with the explode function. $array_homepenalties is the array containing the player's numbers that received penalties $array_homepenmins is the array containing the corresponding penalty minutes So $array_homepenalties[1] is the player number and the number of minutes he received is $array_homepenmins[1]. And it continues this way for each entire array. An example would be: $array_homepenalties = (3, 5, 8, 11) and $array_homepenmins = (2, 4, 5, 2). So this tells me that player #3 received 2 minutes in penalties for this game, #5 got 4 minutes, #8 got 5 minutes, and #11 got 2 mintues. Here is my code for it: //UPDATE Penalty Minutes foreach ($array_homepenalties as $ahp) { $sql_getplayer_pens = "SELECT penaltymins FROM playerstats WHERE jerseynumber = '$ahp' AND teamname = '$hometeam'"; $result_getplayer_pens = mysql_query($sql_getplayer_pens); foreach ($array_homepenmins as $ahpm) { $new_pentotal = $result_getplayer_pens + $ahpm; $sql_updateplayer_pens = "UPDATE playerstats SET penaltymins = '$new_pentotal' WHERE jerseynumber = '$ahp' AND teamname = '$hometeam'"; $result_updateplayer_pens = mysql_query($sql_updateplayer_pens); break; } } So this is the only way that I could come up with to be able to link the two arrays in the way I need. But I am new to php and so I'm not sure if what will happen when I break out of the second foreach. I know that it will go back to the top first foreach loop and go to the 2nd element in $array_homepenalties but I don't know if when it reaches the second foreach loop if it will iterate the second element in $array_homepens because of the break function. Can someone tell me if this will work the way I have it or if there is an easier way to do the same thing? I had thought that there might be a way to do it with muti-dimensional arrays but couldn't figure it out so I came up with this. Thanks for the help. Nick Quote Link to comment https://forums.phpfreaks.com/topic/63362-solved-nested-foreach-loopwill-this-work/ Share on other sites More sharing options...
dbo Posted August 4, 2007 Share Posted August 4, 2007 Reformat that code and paste it in again. I'm not going to bother trying to format the code in order to debug Quote Link to comment https://forums.phpfreaks.com/topic/63362-solved-nested-foreach-loopwill-this-work/#findComment-315781 Share on other sites More sharing options...
ninedoors Posted August 4, 2007 Author Share Posted August 4, 2007 Sorry, it didn't look that way when I preveiwed it. foreach ($array_homepenalties as $ahp) { $sql_getplayer_pens = "SELECT penaltymins FROM playerstats WHERE jerseynumber = '$ahp' AND teamname = '$hometeam'"; $result_getplayer_pens = mysql_query($sql_getplayer_pens); foreach ($array_homepenmins as $ahpm) { $new_pentotal = $result_getplayer_pens + $ahpm; $sql_updateplayer_pens = "UPDATE playerstats SET penaltymins = '$new_pentotal' WHERE jerseynumber = '$ahp' AND teamname = '$hometeam'"; $result_updateplayer_pens = mysql_query($sql_updateplayer_pens); break; } } Quote Link to comment https://forums.phpfreaks.com/topic/63362-solved-nested-foreach-loopwill-this-work/#findComment-315784 Share on other sites More sharing options...
Fadion Posted August 4, 2007 Share Posted August 4, 2007 $new_pentotal = $result_getplayer_pens + $ahpm; that line returns u nothing, as $result_getplayer_pens is just the mysql_query. To retrieve the data u must use mysq_fetch_array within a loop. Smth like: while($values_player_pens = mysql_fetch_array($result_getplayer_pens)){ $new_pentotal = $values_player_pens['columnValue'] + $ahpm; //update query and stuff } Quote Link to comment https://forums.phpfreaks.com/topic/63362-solved-nested-foreach-loopwill-this-work/#findComment-315789 Share on other sites More sharing options...
dbo Posted August 5, 2007 Share Posted August 5, 2007 Well you're dealing with parallel arrays... which can be messy so be careful with them. They are just hard to always keep in sync. But anyways, you shouldn't need the enter loop at all. By looping through one you have the corresponding values in the other, again because they are parallel arrays. $len = count($array_homepenalties); for($i = 0; $i < $len; ++$i) { $sql_getplayer_pens = "SELECT penaltymins FROM playerstats WHERE jerseynumber = '" . $array_homepenalties[$i] . "' AND teamname = '$hometeam'"; $result_getplayer_pens = mysql_query($sql_getplayer_pens); $new_pentotal = $result_getplayer_pens + $array_homepenmins[$i]; $sql_updateplayer_pens = "UPDATE playerstats SET penaltymins = '$new_pentotal' WHERE jerseynumber = '" . $array_homepenalties[$i] . "' AND teamname = '$hometeam'"; $result_updateplayer_pens = mysql_query($sql_updateplayer_pens); } It's very likely that I botched your variable names but hopefully you get the point. Quote Link to comment https://forums.phpfreaks.com/topic/63362-solved-nested-foreach-loopwill-this-work/#findComment-315790 Share on other sites More sharing options...
dbo Posted August 5, 2007 Share Posted August 5, 2007 GuiltyGear also gave some good advice... I didn't read very closely. Between the two of our comments you should be able to get it working correctly. Quote Link to comment https://forums.phpfreaks.com/topic/63362-solved-nested-foreach-loopwill-this-work/#findComment-315791 Share on other sites More sharing options...
ninedoors Posted August 5, 2007 Author Share Posted August 5, 2007 Thanks guys. I'll try those suggestions and see what I get! I'll post here again if I have problems. Nick Quote Link to comment https://forums.phpfreaks.com/topic/63362-solved-nested-foreach-loopwill-this-work/#findComment-315792 Share on other sites More sharing options...
dbo Posted August 5, 2007 Share Posted August 5, 2007 One last tip... rather than use a while loop as GuiltyGear suggested you should just nest that section of code in an if statement (only attempt to run the update if the first query returned results). Quote Link to comment https://forums.phpfreaks.com/topic/63362-solved-nested-foreach-loopwill-this-work/#findComment-315793 Share on other sites More sharing options...
Barand Posted August 5, 2007 Share Posted August 5, 2007 A better array would be one where the player number is the key and the value is the pen mins. Also there is no need to select a record to update it. So you could then have <?php foreach ($array as $ahp => $ahpm) { mysql_query ("UPDATE playerstats SET penaltymins = penaltymins + $ahpm WHERE jerseynumber = '$ahp' AND teamname = '$hometeam' "); } Quote Link to comment https://forums.phpfreaks.com/topic/63362-solved-nested-foreach-loopwill-this-work/#findComment-315801 Share on other sites More sharing options...
dbo Posted August 5, 2007 Share Posted August 5, 2007 Yeah I agree Barand I thought the same thing when I read it but didn't want to change ninedoors approach. Quote Link to comment https://forums.phpfreaks.com/topic/63362-solved-nested-foreach-loopwill-this-work/#findComment-315806 Share on other sites More sharing options...
ninedoors Posted August 5, 2007 Author Share Posted August 5, 2007 That seems way easier. Thanks Barand. As I said I am new to php so I have another question as I haven't worked at all with assoc. arrays. How do you take 2 arrays and make one assoc. array like to one you talked about above? Nick Quote Link to comment https://forums.phpfreaks.com/topic/63362-solved-nested-foreach-loopwill-this-work/#findComment-315810 Share on other sites More sharing options...
Barand Posted August 5, 2007 Share Posted August 5, 2007 You can use array_combine(), but how are you creating the two arrays in the first place? Quote Link to comment https://forums.phpfreaks.com/topic/63362-solved-nested-foreach-loopwill-this-work/#findComment-315813 Share on other sites More sharing options...
dbo Posted August 5, 2007 Share Posted August 5, 2007 Hehe yeah that's why I didn't want to suggest that way Think of it as key and value pars. Here's the easiest example. $hash = Array(); $hash['first'] = "Nick"; $hash['middle'] = "Nicholson"; $hash['last'] = "Nichols"; $name = $hash['first'] + " " + $hash['middle'] + " " + $hash['last']; From there you can get more complicated. $hash = Array(); $hash['name1'] = Array('Nick', 'Nicholson', 'Nichols'); $hash['name2'] = Array('Bob', 'Bobbert', 'Bobbertson'); $cool_name = $hash['name2'][0] + " " + $hash['name2'][1] + " " + $hash['name2'][2]; I hope some of this gets in in the right direction. Quote Link to comment https://forums.phpfreaks.com/topic/63362-solved-nested-foreach-loopwill-this-work/#findComment-315815 Share on other sites More sharing options...
ninedoors Posted August 5, 2007 Author Share Posted August 5, 2007 I get the values from a form and then use explode to turn the values into an array. So for each game there will be a different number of players in the array but the 2 arrays will always have the count value. Nick Quote Link to comment https://forums.phpfreaks.com/topic/63362-solved-nested-foreach-loopwill-this-work/#findComment-315818 Share on other sites More sharing options...
ninedoors Posted August 5, 2007 Author Share Posted August 5, 2007 I get the values from a form and then use explode to turn the values into an array. So for each game there will be a different number of players in the array but the 2 arrays will always have the count value. I meant to say at the bottom there "the 2 arrays will always have the same count value." Nick Quote Link to comment https://forums.phpfreaks.com/topic/63362-solved-nested-foreach-loopwill-this-work/#findComment-315825 Share on other sites More sharing options...
Barand Posted August 5, 2007 Share Posted August 5, 2007 When you get the values from the form, create the single array instead of two. Quote Link to comment https://forums.phpfreaks.com/topic/63362-solved-nested-foreach-loopwill-this-work/#findComment-315836 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.