Jump to content



Photo

Calculate win/loss ratio, how?


  • Please log in to reply
9 replies to this topic

#1 Swift-R

Swift-R

    Aviator

  • Joined: 23-April 05
  • Location: Portugal
  • OS: Windows 8

Posted 17 April 2008 - 21:54

Hello,

I've never calculated ratios before in php and I can't find how to.
I'm trying to calculate the win/loss ratio for my clan matches.

How can I do this?

MySQL table name: egr_matches
important fields: opponent, ourscore, theirscore

Thank you,
Swift-R.


#2 abshack

abshack

    Neowinian

  • Joined: 22-February 08

Posted 18 April 2008 - 15:13

<?php

$host = "your_host_address_or_ip"; //often localhost
$user = "username";
$password = "password";

$mydb = "your_database_name";
mysql_connect($host, $user, $password) or die(mysql_error());
mysql_select_db($mydb) or die(mysql_error());

$sQuery = mysql_query("SELECT ourscore as A, theirscore as B, opponent as C FROM egr_matches");
$wins = 0;
$games = 0;
while($row = mysql_fetch_array($sQuery)){
	 if($row['A'] > $row['B']) {$wins = $wins + 1;}
	 $games = $games + 1;
}

$wlRatio = $wins / $games;

echo "Our win ratio was ".$wlRatio.".";

?>

Written using copy-paste from tutorials... :\

#3 OP Swift-R

Swift-R

    Aviator

  • Joined: 23-April 05
  • Location: Portugal
  • OS: Windows 8

Posted 18 April 2008 - 17:00

Is ratio supposed to be a percentage? Because it results numbers like 0.682823... I have like a 6:3 ratio.

#4 Rudy

Rudy

    Neowinian Senior

  • Joined: 30-September 01
  • Location: Ottawa, On

Posted 18 April 2008 - 17:07

you calculate it like this:

games won / games played

you'll get something like 0.xx and you can get the percentage by * 100

#5 OP Swift-R

Swift-R

    Aviator

  • Joined: 23-April 05
  • Location: Portugal
  • OS: Windows 8

Posted 18 April 2008 - 17:08

Yes I got it, thanks ;D

$winsratio = round(($wins / $games) * 100, 0);


#6 abshack

abshack

    Neowinian

  • Joined: 22-February 08

Posted 18 April 2008 - 19:34

soooo... did it work? :D
that was totally googled & not tested code, btw.. :)

#7 OP Swift-R

Swift-R

    Aviator

  • Joined: 23-April 05
  • Location: Portugal
  • OS: Windows 8

Posted 18 April 2008 - 20:00

Yes it worked perfectly, click in my signature' banner and check Matches page if you want to see it working.

However, I've multiplied it to 10 instead of 100 to give a ratio like 7:3 instead of 73:32. :)

Thank you very much.

#8 abshack

abshack

    Neowinian

  • Joined: 22-February 08

Posted 18 April 2008 - 20:02

yay... always glad my Googling was of use.. :)

#9 +LogicalApex

LogicalApex

    Software Engineer

  • Tech Issues Solved: 5
  • Joined: 14-August 02
  • Location: Philadelphia, PA
  • OS: Windows 7 Ultimate x64
  • Phone: Nexus 5

Posted 20 April 2008 - 13:40

Yes I got it, thanks ;D

$winsratio = round(($wins / $games) * 100, 0);



Yes it worked perfectly, click in my signature' banner and check Matches page if you want to see it working.

However, I've multiplied it to 10 instead of 100 to give a ratio like 7:3 instead of 73:32. :)

Thank you very much.


I'm not sure your ratio code is accurate or maybe you're just not simplifying your fraction?

If you're going to display a ratio as 1:2 that would be equal to 1/2 so you wouldn't need to do any math on it like your doing unless to simplify the fraction to its lowest terms...

#10 OP Swift-R

Swift-R

    Aviator

  • Joined: 23-April 05
  • Location: Portugal
  • OS: Windows 8

Posted 21 April 2008 - 16:17

I'm not sure your ratio code is accurate or maybe you're just not simplifying your fraction?

If you're going to display a ratio as 1:2 that would be equal to 1/2 so you wouldn't need to do any math on it like your doing unless to simplify the fraction to its lowest terms...


$ = ROUND(x, 0)
1:2 means we won 1 match in every two matches.

;)



Click here to login or here to register to remove this ad, it's free!