karimali831 Posted November 20, 2011 Share Posted November 20, 2011 Hey, Can someone please fix my code below? I simply want to add all values for $row[score1] in first if statement and add all values for $row[score2] in second if statement and then add them together to return final value . while( $row = mysql_fetch_assoc($query)) { if($clanID==$row[clan1] && $row[score1] > $row[score2]) { $points[] = $row[score1]; } if($clanID==$row[clan2] && $row[score1] < $row[score2]) { $points[] = $row[score2]; } } return $points; . So lets say I echo the variables that I want added... if($clanID==$row[clan1] && $row[score1] > $row[score2]) { $points[] = $row[score1]; echo $row['score1']."<br>"; } if($clanID==$row[clan2] && $row[score1] < $row[score2]) { $points[] = $row[score2]; echo $row['score2']."<br>"; } On page it returns as: 11 16 14 Add it together is 41 and this is what I want the output to be. Hope you understand I appreciate any help! P.S. I must be able to return final value OUTSIDE loop. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/251500-simple-arrayadd-question/ Share on other sites More sharing options...
Alexv Posted November 20, 2011 Share Posted November 20, 2011 I don't understand. Do you want only 41 to be your output or all values and then 41? Quote Link to comment https://forums.phpfreaks.com/topic/251500-simple-arrayadd-question/#findComment-1289828 Share on other sites More sharing options...
karimali831 Posted November 20, 2011 Author Share Posted November 20, 2011 When you add all the values it will be 41 so I just want the output to be 41 so it is like: while() { $row['col1']; -- sum up all values in $row['col1'] } Quote Link to comment https://forums.phpfreaks.com/topic/251500-simple-arrayadd-question/#findComment-1289830 Share on other sites More sharing options...
Alexv Posted November 20, 2011 Share Posted November 20, 2011 $points = 0; while( $row = mysql_fetch_assoc($query)) { if($clanID==$row[clan1] && $row[score1] > $row[score2]) { $points += $row[score1]; } if($clanID==$row[clan2] && $row[score1] < $row[score2]) { $points += $row[score2]; } } return $points; That should return the sum of all values. Quote Link to comment https://forums.phpfreaks.com/topic/251500-simple-arrayadd-question/#findComment-1289831 Share on other sites More sharing options...
karimali831 Posted November 20, 2011 Author Share Posted November 20, 2011 $points = 0; while( $row = mysql_fetch_assoc($query)) { if($clanID==$row[clan1] && $row[score1] > $row[score2]) { $points += $row[score1]; } if($clanID==$row[clan2] && $row[score1] < $row[score2]) { $points += $row[score2]; } } return $points; That should return the sum of all values. Brilliant thanks very much! I thought it didn't work because $points = 0; wasn't declared above loop, and was getting all crazy values. why must this be done? Quote Link to comment https://forums.phpfreaks.com/topic/251500-simple-arrayadd-question/#findComment-1289834 Share on other sites More sharing options...
Alexv Posted November 20, 2011 Share Posted November 20, 2011 Just good coding practice. You can do it without that line but if you turn on all errors php will give a Notice about an undefined variable $points. It will still work but i just like it to be by the book Quote Link to comment https://forums.phpfreaks.com/topic/251500-simple-arrayadd-question/#findComment-1289837 Share on other sites More sharing options...
Pikachu2000 Posted November 20, 2011 Share Posted November 20, 2011 It should also throw a ton of "undefined constant" notices, too. None of the string type array indices are quoted in the OP's code. Quote Link to comment https://forums.phpfreaks.com/topic/251500-simple-arrayadd-question/#findComment-1289839 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.