I am pulling values from a data table and ranking them. My data table has the following columns:
value | statistic_id | player_id | day | id
The value is the value for the particular statistic. It is a number
The statistic_id is the statistic being tracked. It is a number.
The player_id is the id for a player. It is a number.
The day is the day the value is inserted.
The id is an auto_increment index.
I am trying to rank and then filter my data. I've figured out how to rank the data. Here is the code to rank my data:
Expand|Select|Wrap|Line Numbers
- <?
- $statistic_id = $_GET['s'];
- $pid = $_GET['p'];
- include 'index.php';
- $sql = sprintf('SELECT * FROM data WHERE day = CURdate()-INTERVAL 1 day AND statistic_id = %d', $statistic_id);
- $result = mysql_query( $sql );
- $array = array();
- while( $obj = mysql_fetch_object( $result ) ) {
- $array[ $obj->player_id ] = $obj->value;
- }
- function getNameFromId ( $player_id ) {
- $sql = sprintf( 'SELECT name FROM players WHERE id = %d', $player_id );
- $res = mysql_query( $sql );
- $arr = mysql_fetch_array( $res );
- return $arr[ 'name' ];
- }
- arsort( $array );
- $i = 0;
- foreach ( $array as $player_id => $value ) {
- $i++;
- $player_name = getNameFromId ( $player_id );
- echo sprintf( "\n%s is ranked %d with a value of %s <br/>", $player_name, $i, $value );
- }
- ?>
value | statistic_id | player_id | day | id
1 | 1 | 1 | 2010| 1
2 | 1 | 2 | 2010| 2
3 | 2 | 1 | 2010| 3
2 | 2 | 2 | 2010| 4
Given this data, I have already been able to rank by statistic_id 1 so that the order goes
1) player_id 2 - value
2) player_id 1 - value
I would then like to filter by statistic_id 2 so that only values greater than 2 show in my ranked list based on statistic_id 1. Given this filter, only player_id 1 would show:
1) player_id 1 - value
If you need more clarification, please ask. If you have any methods on how this can be accomplished I would be very appreciative!
Thanks!