Jump to content

Simple array/add question


karimali831

Recommended Posts

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

.

:shrug:

 

        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.

Link to comment
https://forums.phpfreaks.com/topic/251500-simple-arrayadd-question/
Share on other sites

        $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.

 

 

        $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! :D

 

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?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.