Jump to content

[SOLVED] Randomize and rank


Aeolus

Recommended Posts

So what I'm wanting to do is generate a randomized (well, it will be partially randomized anyway, but that's another story) number (score) for each player, and then rank players according to that score. I want to store the rank for each player, number of players entered, and the scores for each player into a table. This would be for like a weekly contest basically.

 

I know I can use the php random # command to create my score, but I don't know how to rank the outputs. I know little to nothing about arrays, but I'm pretty sure that's where I need to begin. I'm sure I can read all I want about arrays - but how would I do what I'm trying to with an array?

 

Any ideas? If I just get pointed in the right direction I would appreciate it greatly.

Link to comment
Share on other sites

I don't quite understand why you are using a random number to rank people's scores? A random number is like the polar opposite of a rank. And how exactly are you going to be using arrays. Are you going to store the data from a table into an array to output the data on the table? If so:

 

here is a link on sorting arrays: http://us3.php.net/manual/en/function.sort.php

 

You can get the number of players entered into the database by doing using the COUNT mysql function, or the mysql_num_rows php function. Do a google search for those for more info on them

 

to get the rank of a player, you will probably want to get the scores from the table, and store those into an array. THen sort the array from highest to lowest (or lowest to highest) and then see compare the scores until you find where the particular score belongs, and then you have your rank. You can use the array_splice function to put a value into any part of the array

 

http://us2.php.net/manual/en/function.array-splice.php

 

Hope that helps!

Link to comment
Share on other sites

I'll take a look at the links you posted, thanks!

 

To answer your questions...

 

Players will have a score randomized then their rank will be applied based off the score they received. So the player with the highest score would be ranked 1st, then 2nd, etc. etc.

 

I need to insert the rank and the score they got into a "result" table. I wouldn't mind using a temporary table in there for the score, and I would drop that table at the end of the script.

 

Does that make any more sense?

Link to comment
Share on other sites

if i understand what you are trying to do your already inserting your values in to the table so this should work:

 

$q = mysql_query("SELECT * FROM tblname ORDER BY score");
//change score to match the colum the scores in
$i = 0;
//only if storeing in a array
while($results = mysql_fetch_array($q)){
    //code to out output the scores
    echo $results['score'];
    //or store into an array
    $array[$i] = $results['score'];
    $i++;
}

Link to comment
Share on other sites

well you would need the score to compare scores that are entered into the table at other times, unless everyone's scores are all entered at the same time, so I wouldn't suggest using a temp table.

 

I still don't understand why their scores are randomized, but that doesn't really matter all to much, i'm sure you have a reason.

 

However, as far as storing the rank goes, I would just store the score, and then figure out the rank whenever you output it (on rank.php or whatever) Because everytime a new score is added, some to all of the ranks on the table will change, resulting in having to update the table every time you add a new entry. This is true only if you DONT enter all the scores at the same time of course

 

 

Oh wow.. completely forgot about ORDER BY. yeah use that instead haha. you could also use that to update the ranks.

Link to comment
Share on other sites

The scores will be entered at the same time, that's what I'm trying to do anyway. And the scores will only be partially randomized, I'll be actually adding a randomized number to a previously existing score (so the random number is kind of an offset of what they already have).

 

So.. [not in real coding here, obv]...

 

get from table $previousscore for each player

 

{for each player $previousscore + $randomscore = $finalscore}

>> here is where I might use the temp table, to insert $finalscore for each player, and then get $finalscore from the temp table to do the ranking

 

rank players by $finalscore

 

insert into table $playername, $finalscore, rank

Link to comment
Share on other sites

$qscores = mysql_query("SELECT * FROM tblname");
$i=0;
while($previousscore = mysql_fetch_array($qscores)){
     $random = "";//set random score
     $final = $previousscore['score'] + $random;
     mysql_query("INSERT INTO tblname (score) VALUE($final)");
}
//retrive results 
$q = mysql_query("SELECT * FROM tblname ORDER BY score");
//change score to match the colum the scores in
$i = 0;
//only if storeing in a array
while($results = mysql_fetch_array($q)){
    //code to out output the scores
    echo $results['score'];
    //or store into an array
    $array[$i] = $results['score'];
    $i++;
}

Link to comment
Share on other sites

you can circumvent using a temp table by storing the final score into an array, and sorting the array by the final score (you can of course make an associative array with the key as the player name).

You want to use arsort, so you can maintain the index association, and so it goes in reverse order (highest to lowest)

 

then using a foreach, after sorting the array, you can place the data into the table. kind of like

 

//sample array

$player_data = array("player1" => 1000, "player2" => 2004, "player3" =>1003, "player4" => 1004, "player5" => 3054, "player6" => 435);

arsort($player_data, SORT_NUMERIC);
$i = 1; //the rank
foreach($player_data as $key=>$value){
echo $key . "'s score: " . $value;

$sql = "insert into table (rank, player, score) VALUES ($i, $key, $value)";
$query = mysql_query($sql);
$i++;
}

 

the output of the above would be (without the sql of course)

 

number 1: player5's score: 3054

number 2: player2's score: 2004

number 3: player4's score: 1004

number 4: player3's score: 1003

number 5: player1's score: 1000

number 6: player6's score: 435

 

Hope that helps!

 

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.