Aeolus Posted August 18, 2009 Share Posted August 18, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/170842-solved-randomize-and-rank/ Share on other sites More sharing options...
mikesta707 Posted August 18, 2009 Share Posted August 18, 2009 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! Quote Link to comment https://forums.phpfreaks.com/topic/170842-solved-randomize-and-rank/#findComment-901025 Share on other sites More sharing options...
Aeolus Posted August 18, 2009 Author Share Posted August 18, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/170842-solved-randomize-and-rank/#findComment-901038 Share on other sites More sharing options...
ldb358 Posted August 18, 2009 Share Posted August 18, 2009 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++; } Quote Link to comment https://forums.phpfreaks.com/topic/170842-solved-randomize-and-rank/#findComment-901039 Share on other sites More sharing options...
mikesta707 Posted August 18, 2009 Share Posted August 18, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/170842-solved-randomize-and-rank/#findComment-901041 Share on other sites More sharing options...
Aeolus Posted August 18, 2009 Author Share Posted August 18, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/170842-solved-randomize-and-rank/#findComment-901048 Share on other sites More sharing options...
ldb358 Posted August 18, 2009 Share Posted August 18, 2009 $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++; } Quote Link to comment https://forums.phpfreaks.com/topic/170842-solved-randomize-and-rank/#findComment-901062 Share on other sites More sharing options...
mikesta707 Posted August 18, 2009 Share Posted August 18, 2009 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! Quote Link to comment https://forums.phpfreaks.com/topic/170842-solved-randomize-and-rank/#findComment-901075 Share on other sites More sharing options...
Aeolus Posted August 18, 2009 Author Share Posted August 18, 2009 That's working great for displaying on a page, can't get it to insert into a database yet but give me a minute to fidget with it Quote Link to comment https://forums.phpfreaks.com/topic/170842-solved-randomize-and-rank/#findComment-901087 Share on other sites More sharing options...
mikesta707 Posted August 18, 2009 Share Posted August 18, 2009 are you getting any SQL errors? What does your code look like? Quote Link to comment https://forums.phpfreaks.com/topic/170842-solved-randomize-and-rank/#findComment-901091 Share on other sites More sharing options...
Aeolus Posted August 18, 2009 Author Share Posted August 18, 2009 Works perfectly now Same thing you had, but I changed $sql = "insert into events (rank, score, player) VALUES ($i, $value, $key)"; to $sql = "insert into events (rank, score, player) VALUES ('$i', '$value', '$key')"; Quote Link to comment https://forums.phpfreaks.com/topic/170842-solved-randomize-and-rank/#findComment-901093 Share on other sites More sharing options...
mikesta707 Posted August 18, 2009 Share Posted August 18, 2009 Ahh I see. I forget the single quotes alot . Make sure to mark the topic is solved! Quote Link to comment https://forums.phpfreaks.com/topic/170842-solved-randomize-and-rank/#findComment-901095 Share on other sites More sharing options...
Aeolus Posted August 18, 2009 Author Share Posted August 18, 2009 No I just need to get all the players and their scores into the array without inserting them one by one... ie getting them from the table of players entered into the contest.. Thanks for all your help! Quote Link to comment https://forums.phpfreaks.com/topic/170842-solved-randomize-and-rank/#findComment-901100 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.