//display a table with top 10 players and their results
function statistics($conn){
//count the number of games winned by every winner and order desc
$resCount=db_query("SELECT winner_id, COUNT(winner_id) AS top_win FROM games WHERE winner_id!='1' GROUP BY winner_id ORDER BY top_win DESC LIMIT 0,10",$conn);
$gamesWonNum=mysql_num_rows($resCount);
// if there is some value then initiate the table then nothing is display
if ($gamesWonNum != 0) {
$html='<p class="table_title">Statistics </p>
<table class="table_game">
<tr class="x">
<th class="table_header_games">Name</th>
<th class="table_header_games">Games played</th>
<th class="table_header_games">Win</th>
<th class="table_header_games">Draw</th>
<th class="table_header_games">Lost</th>
</tr>';
//$table_row_class - diferent class for odd and even rows
$table_row_class = "table_odd_row_games";
while ($gamesWon=mysql_fetch_assoc($resCount)){
//change the class for even and odd rows
$table_row_class = ($table_row_class=="table_odd_row_games")?"table_even_row_games":"table_odd_row_games";
//select name for every winner form top 10
$resN=db_query("SELECT name FROM users WHERE user_id='".$gamesWon['winner_id']."' AND name != 'Draw'",$conn);
$rowN = mysql_fetch_assoc($resN);
//display name
$html.='<tr class="'.$table_row_class.'"><td>'.$rowN['name'].'</td>';
//find sub of the games played (gameP) by a winner as a player_1 (user_id_1) or as a player_2 (user_id_2)
$resG=db_query("SELECT SUM(c) AS gameP FROM(
SELECT COUNT(user_id_1) AS c FROM games AS games WHERE user_id_1=".$gamesWon['winner_id']."
UNION
SELECT COUNT(user_id_2) AS c FROM games AS games WHERE user_id_2=".$gamesWon['winner_id']."
) AS total",$conn);
while ($rowG=mysql_fetch_assoc($resG)){
//display numbers of games played
$html.='<td>'.$rowG['gameP'].'</td>';
//calculate procent of win
$procentWin=round(($gamesWon['top_win']*100)/$rowG['gameP'],2);
//display numbers and procent of games winned
$html.='<td>'.$gamesWon['top_win'].' ('.$procentWin.'%) </td>';
//count the numbers of games finished as draw
$resD=db_query("SELECT COUNT(winner_id) AS gameDraw FROM games WHERE (user_id_1=".$gamesWon['winner_id']." OR user_id_2=".$gamesWon['winner_id'].") AND winner_id='1'",$conn);
$rowD=mysql_fetch_assoc($resD);
if ($rowD['gameDraw']=='0'){
//if game draw is 0 then the procent draw is 0 too (cannot calculate with divide by 0)
$procentDraw=0;
}else{
//calculate procent draw if is not 0
$procentDraw=round(($rowD['gameDraw']*100)/$rowG['gameP'],2);
}
//display numbers and procent of games finished by draw
$html.='<td>'.$rowD['gameDraw'].' ('.$procentDraw.'%) </td>';
//$GLOBALS['db_in_debug']=1;
//calculate numbers of games lost
$gameLost=$rowG['gameP']-($gamesWon['top_win']+$rowD['gameDraw']);
if($gameLost=='0'){
//if game lost is 0 then the procent of games lost is 0 too (cannot calculate with divide by 0)
$procentLost=0;
}else{
//calculate procent of losts games if is not 0
$procentLost=round(($gameLost*100)/$rowG['gameP'],2);
}
$html.='<td>'.$gameLost.' ('.$procentLost.'%)</td></tr>';
}
}
}
$html.='</table></br>';
return $html;
}