rbragg Posted May 3, 2007 Share Posted May 3, 2007 Is there a way to do something like this: <?php foreach ($_POST['hits'] as $playerID => $hit, $_POST['ab'] as $playerID => $ab , $_POST['bb'] as $playerID => $bb) { } ?> Quote Link to comment https://forums.phpfreaks.com/topic/49868-foreach-handling-multiple-arrays/ Share on other sites More sharing options...
Barand Posted May 3, 2007 Share Posted May 3, 2007 No. What are you trying to do? Quote Link to comment https://forums.phpfreaks.com/topic/49868-foreach-handling-multiple-arrays/#findComment-244662 Share on other sites More sharing options...
rbragg Posted May 3, 2007 Author Share Posted May 3, 2007 Hi there, Barand! foreach ($_POST['hits'] as $playerID => $hit) For example, I have $_POST['hits'], $_POST['ab'], $_POST['bb']. Each of these are arrays (containing the number of hits for each playerID, the number of at bats for each playerID, and the number of walks for each playerID). Each has its own column in my db table. playerID is also a column and I want to store the values for each playerID. <?php # inserting stats for new tourny $player = $_POST['playerID']; foreach ($player as $order => $playerID){ $queryStatsPer = " INSERT into stats_per_tourny (playerID, tournyID, hit, ab, bb) VALUES ( '$playerID', '".$_POST['tournyID']."', '$hit', '$ab', '$bb' )"; $insertStatsPer = mysql_query($queryStatsPer) or die("<p class='style5'>Insert query failed: " . mysql_error()) . "</p>"; } ?> Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/49868-foreach-handling-multiple-arrays/#findComment-244672 Share on other sites More sharing options...
Barand Posted May 3, 2007 Share Posted May 3, 2007 foreach ($_POST['playerID'] as $k => $playerID) { $hits = $_POST['hits'][$k]; $ab = $_POST['ab'][$k]; $bb = $_POST['bb'][$k]; $queryStatsPer = " INSERT into stats_per_tourny (playerID, tournyID, hit, ab, bb) VALUES ( '$playerID', '".$_POST['tournyID']."', '$hits', '$ab', '$bb' )"; $insertStatsPer = mysql_query($queryStatsPer) or die("<p class='style5'>Insert query failed: " . mysql_error()) . "</p>"; } Quote Link to comment https://forums.phpfreaks.com/topic/49868-foreach-handling-multiple-arrays/#findComment-244681 Share on other sites More sharing options...
rbragg Posted May 4, 2007 Author Share Posted May 4, 2007 You are continually awesome! I'm trying to apply this idea when using operations. I'm doing a query to get values currently stored in a db table. I am adding the posted values to those and then doing an update query. Here is what I have so far: <?php while($all = mysql_fetch_array( $allstats )) { foreach ($player as $id => $playerID) { $hit = $_POST['hit'][$id]; $allhit = $all['hit'] + $hit; $ab = $_POST['ab'][$id]; $allab = $all['ab'] + $ab; $bb = $_POST['bb'][$id]; $allbb = $all['bb'] + $bb; $secondb = $_POST['2b'][$id]; $all2b = $all['2b'] + $secondb; $thirdb = $_POST['3b'][$id]; $all3b = $all['3b'] + $thirdb; $hr = $_POST['hr'][$id]; $allhr = $all['hr'] + $hr; $rbi = $_POST['rbi'][$id]; $allrbi = $all['rbi'] + $rbi; $queryUpdateAll = " UPDATE stats_all SET hit = '$allhit' , ab = '$allab', bb = '$allbb', 2b = 'all2b', 3b = 'all3b', hr = '$allhr', rbi = '$allrbi' WHERE playerID = '$player' "; $updateAll = mysql_query($queryUpdateAll) or die("<p class='style5'>Update query failed: " . mysql_error()) . "</p>"; } ?> This doesn't seem to be working. ??? Quote Link to comment https://forums.phpfreaks.com/topic/49868-foreach-handling-multiple-arrays/#findComment-245337 Share on other sites More sharing options...
Barand Posted May 4, 2007 Share Posted May 4, 2007 Something like <?php foreach ($_POST['playerID'] as $k => $playerID) { $hits = $_POST['hits'][$k]; $ab = $_POST['ab'][$k]; $bb = $_POST['bb'][$k]; $sql = "UPDATE stats_all SET hits = hits + $hits, ab = ab + $ab, bb = bb + $bb WHERE playerID = '$playerID' "; ?> Quote Link to comment https://forums.phpfreaks.com/topic/49868-foreach-handling-multiple-arrays/#findComment-245342 Share on other sites More sharing options...
rbragg Posted May 4, 2007 Author Share Posted May 4, 2007 I wouldn't use the $all fetch_array from the original select query? Quote Link to comment https://forums.phpfreaks.com/topic/49868-foreach-handling-multiple-arrays/#findComment-245358 Share on other sites More sharing options...
Barand Posted May 4, 2007 Share Posted May 4, 2007 Wastes time getting the existing values, incrementing them, then updating when you can update them directly. Unless, of course, you need the array for something else, such as display. In your update query, $player is your array data, so WHERE playerID = '$player' should be WHERE playerID = '$playerID' Quote Link to comment https://forums.phpfreaks.com/topic/49868-foreach-handling-multiple-arrays/#findComment-245366 Share on other sites More sharing options...
rbragg Posted May 4, 2007 Author Share Posted May 4, 2007 I understand what you've done. Thank you. Can you perform only limited operations within a query? Before, I was calculating an average outside of the query like this: <?php $decimal = @($hit / $ab); # @ prevents division by zero error $avg = round($decimal,3); # round to 3 decimal places ?> I'm trying this: SET avg = round( @($hit/$ab),3 ) ... and of course I get an error. Quote Link to comment https://forums.phpfreaks.com/topic/49868-foreach-handling-multiple-arrays/#findComment-245398 Share on other sites More sharing options...
rbragg Posted May 4, 2007 Author Share Posted May 4, 2007 You know I have to make things more complicated than they really are. The operation works fine outside of the query. <?php $decimal = @($hit / $ab); # @ prevents division by zero error $avg = round($decimal,3); # round to 3 decimal places $queryUpdateAll = " UPDATE stats_all SET hit = hit + '$hit', ab = ab + '$ab', bb = bb + '$bb', 2b = 2b + '$secondb', 3b = 3b + '$thirdb', hr = hr + '$hr', rbi = rbi + '$rbi', avg = '$avg' WHERE playerID = '$playerID' "; ?> This works perfectly fine. Thanks for all of your help as usual. Quote Link to comment https://forums.phpfreaks.com/topic/49868-foreach-handling-multiple-arrays/#findComment-245455 Share on other sites More sharing options...
Barand Posted May 4, 2007 Share Posted May 4, 2007 $decimal = @($hit / $ab); # @ prevents division by zero error No, it just stops it being reported, use $decimal = $ab==0 ? 0 : ($hit / $ab); # prevents division by zero error Quote Link to comment https://forums.phpfreaks.com/topic/49868-foreach-handling-multiple-arrays/#findComment-245631 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.