calculating ranks in php - loop?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
blade_922
Forum Contributor
Posts: 132
Joined: Wed Jul 12, 2006 4:57 pm

calculating ranks in php - loop?

Post by blade_922 »

Hey i have a script which isnt running properly. Its ment to calculate the new position of a team after a win has been reported.

heres the code

Code: Select all

//FIND WINNERS NEW RANK

$totalranked=mysql_query("SELECT COUNT(*) FROM ladder_$ladder[id] WHERE rank > '0'");
$totalranked=mysql_fetch_array($totalranked);
$totalranked="$totalranked[0]";

$adjustranks="";


if($winner[rank] > 0){
 if(($winner[rank] > $loser[rank]) && ($loser[rank] > 0)){
 $middlerank=(($winner[rank] + $loser[rank]) - 0.5);
 $new_winner_rank=round(($middlerank / 2) - 0.5);
 $adjustranks=1;
 }else{
 $new_winner_rank="$winner[rank]";
 }
}
else{
$new_winner_rank=($totalranked + 1);
$adjustranks=1;
}


So if Team F plays a match and they originally has 100 points. From their win, they now (for example) have 198 points.

Previously, the ranking were as follows:

Rank 1: Team A had 220 points
Rank 2: Team B had 210 points
Rank 3: Team C had 200 points
Rank 4: Team D had 190 points
Rank 5: Team E had 180 points
Rank 6: Team F had 100 points
Rank 7: Team G had 90 points

Now:

Rank 1: Team A has 220 points
Rank 2: Team B has 210 points
Rank 3: Team C has 200 points
Rank 4: Team F has 198 points
Rank 5: Team D has 190 points
Rank 6: Team E has 180 points
Rank 7: Team G has 90 points

It stores there new rank as 4 in the database.

MY current script above wont work. It keeps giving wrong ranks . Can someonle look at this for me.

Regards
Last edited by blade_922 on Wed Sep 05, 2007 1:19 pm, edited 1 time in total.
blade_922
Forum Contributor
Posts: 132
Joined: Wed Jul 12, 2006 4:57 pm

Post by blade_922 »

anyone see the problem with the code?

regards
blade_922
Forum Contributor
Posts: 132
Joined: Wed Jul 12, 2006 4:57 pm

Post by blade_922 »

Hey im in a rush to get this fixed.

Would i use a loop or something? I know what i am trying to do, but putting it into php just doesnt work.

Anyone help me out?
blade_922
Forum Contributor
Posts: 132
Joined: Wed Jul 12, 2006 4:57 pm

Post by blade_922 »

what im ytrying to do is

If Team F plays a match and they originally has 100 points. From their win, they now (for example) have 198 points.

Previously, the ranking were as follows:

Rank 1: Team A had 220 points
Rank 2: Team B had 210 points
Rank 3: Team C had 200 points
Rank 4: Team D had 190 points
Rank 5: Team E had 180 points
Rank 6: Team F had 100 points
Rank 7: Team G had 90 points

Now:

Rank 1: Team A has 220 points
Rank 2: Team B has 210 points
Rank 3: Team C has 200 points
Rank 4: Team F has 198 points
Rank 5: Team D has 190 points
Rank 6: Team E has 180 points
Rank 7: Team G has 90 points

It stores there new rank as 4 in the database.

MY current script above wont work. It keeps giving wrong ranks . Can someonle look at this for me.
Begby
Forum Regular
Posts: 575
Joined: Wed Dec 13, 2006 10:28 am

Post by Begby »

Why are you storing the ranks in the database? Instead you can calculate the rank pretty easily with a query something like this

Code: Select all

SELECT l1.*, 
(SELECT COUNT(*) FROM ladder l2 WHERE points > 0 and l2.points < l1.points) as rank
FROM ladder l1
Also, there is no reason to be storing each ladder in a new table if the fields are the same. Instead you should have a ladder field that indicates which ladder the record belongs to
Post Reply