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

 

 

Link to comment
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.

 

 

 

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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.