Jump to content

Performing addition to returned results ?


j33baS

Recommended Posts

Hi i've been searching around for a way to perform an addition to all the reults from a while loop but cant really crack it and was wondering if anyone could point me in the right direction ! If i have a table like this ...

 

SCORES (playername, score)

Player1 - 3

Player1 - 5

Player1 - 1

Player1 - 6

Player2 - 2

Player2 - 3

 

and a query like this ...

 

<?php
$sql = "SELECT * FROM scores where playername = $playername";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)){
echo "playername and score...";
}
?>

 

will output fro "player1" ...

 

Player1 - 3
Player1 - 5
Player1 - 1
Player1 - 6

 

what can i do to get all those scores added up ? so i can have ...

 

total = 15

 

ive been messing around making the "score" variable  = to a variable $x and trying to do $x++ or sumthing ? do i have to make some sort of function ? would appreciate any help, cheers !

Link to comment
Share on other sites

<?php
$sql = "SELECT * FROM scores where playername = $playername";
$result = mysql_query($sql);
$score=0;
while ($row = mysql_fetch_array($result)){
//do rest here 
$score+=$row["score"];//where score is the column name in the database
}
echo "Total".$score;
?>

Hope this works for you.

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.