Jump to content

PHP creating ranks


Petsmacker

Recommended Posts

Basically I have a website where people are sorted into one of 4 teams when they join and throughout 2 months they earn points - the team with most points at the end of the month are the winners - easy.

What I want is Something like this:
[table][tr][td]TEAM[/td][td]Team 1[/td][td]Team 2[/td][td]Team 3[/td][td]Team 4[/td][/tr][tr][td]POS.[/td][td][center]3[/center][/td][td][center]1[/center][/td][td][center]2[/center][/td][td][center]4[/center][/td][/tr][/table]

So POS would mean what position they had come at the end of the month when I close the game. The only problem is that sometimes houses can get the same amount of points so in that case the positions should be like:
[table][tr][td]TEAM[/td][td]Team 1[/td][td]Team 2[/td][td]Team 3[/td][td]Team 4[/td][/tr][tr][td]POS.[/td][td][center]2[/center][/td][td][center]1[/center][/td][td][center]2[/center][/td][td][center]3[/center][/td][/tr][/table]
for example

I've tried using tons of IF statements but when it came to the houses having equal points in some instances it made things too complicated for me. I hope somebody can help!
Link to comment
Share on other sites

Make a column in the user table.. named 'team'
and make it enter a random number (for the team, 1-4)

then.. make a table 'points'
and make 4 columns.. Team 1, Team 2, Team 3, and Team 4.
and thisz will be the totals for all of them.
so, if you want to give team 1 a point, you +1 to the team 1 column.. and, such.

if you want to record each users contribution, make a 'points' column in the USER table.
and, then make it +1 everytime they contribute a point ALSO make sure it +1's their teams points aswell.

I hope that wasn't too confusing...
Link to comment
Share on other sites

The layout of my database doesn't work like that, if it did I really wouldn't have a problem.
Each user in their row of the database has what team they're in and how many points they've personally earnt this month. This is then tallied up for each person in each team to come to the team totals. So I can't do it that way. And plus, even if I did do it in descending order, it would allow team positions such as 1,2,3,3 if two teams had the same amount of points.
Link to comment
Share on other sites

Hi,
Hope my code is right for you.
[code]
<?php
//example data
$arr = array("team1"=>200, "team2"=>100, "team3"=>200, "team4"=>300);

//sort and array in reverse order
arsort($arr);

//define score
$score = 5;

//loop
foreach($arr as $key=>$val){
if($tmp_val!=$val){
$score--;
$d[$key]=$score;
}else{
$d[$key]=$score;
}
$tmp_val = $val;
}

//checking value
print_r($d);
?>
[/code]

You may modify some code for forward order:
- arsort($arr); to asort($arr);
- $score = 5; to $score = 0;
- $score--; to $score++;

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.