Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 1st, 2012  |  syntax: PHP  |  size: 3.42 KB  |  hits: 15  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1.  
  2. //display a table with top 10 players and their results
  3. function statistics($conn){
  4.         //count the number of games winned by every winner and order desc
  5.         $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);
  6.         $gamesWonNum=mysql_num_rows($resCount);
  7.         // if there is some value then initiate the table then nothing is display
  8.         if ($gamesWonNum != 0) {
  9.                 $html='<p class="table_title">Statistics </p>
  10.                         <table  class="table_game">
  11.                                         <tr class="x">
  12.                                                 <th class="table_header_games">Name</th>
  13.                                                 <th class="table_header_games">Games played</th>
  14.                                                 <th class="table_header_games">Win</th>
  15.                                                 <th class="table_header_games">Draw</th>
  16.                                                 <th class="table_header_games">Lost</th>
  17.                                         </tr>';
  18.                 //$table_row_class  - diferent class for odd and even rows             
  19.                 $table_row_class = "table_odd_row_games";                      
  20.                 while ($gamesWon=mysql_fetch_assoc($resCount)){
  21.                         //change the class for even and odd rows
  22.                         $table_row_class = ($table_row_class=="table_odd_row_games")?"table_even_row_games":"table_odd_row_games";
  23.                         //select name for every winner form top 10
  24.                         $resN=db_query("SELECT name FROM users WHERE user_id='".$gamesWon['winner_id']."' AND name != 'Draw'",$conn);
  25.                         $rowN = mysql_fetch_assoc($resN);
  26.                         //display name
  27.                         $html.='<tr class="'.$table_row_class.'"><td>'.$rowN['name'].'</td>';
  28.                         //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)                                      
  29.                         $resG=db_query("SELECT SUM(c) AS gameP FROM(   
  30.                                                                                                 SELECT COUNT(user_id_1) AS c FROM games AS games WHERE user_id_1=".$gamesWon['winner_id']."
  31.                                                                                                 UNION
  32.                                                                                                 SELECT COUNT(user_id_2) AS c FROM games AS games WHERE user_id_2=".$gamesWon['winner_id']."
  33.                                                                                                 ) AS total",$conn);
  34.                                                                                                
  35.                         while ($rowG=mysql_fetch_assoc($resG)){
  36.                                 //display numbers of games played
  37.                                 $html.='<td>'.$rowG['gameP'].'</td>';
  38.                                
  39.                                 //calculate procent of win
  40.                                 $procentWin=round(($gamesWon['top_win']*100)/$rowG['gameP'],2);
  41.                                 //display numbers and procent of games winned  
  42.                                 $html.='<td>'.$gamesWon['top_win'].' ('.$procentWin.'%) </td>';
  43.                                 //count the numbers of games finished as draw                                                  
  44.                                
  45.                                 $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);         
  46.                                 $rowD=mysql_fetch_assoc($resD);
  47.                                 if ($rowD['gameDraw']=='0'){
  48.                                         //if game draw is 0 then the procent draw is 0 too (cannot calculate with divide by 0)
  49.                                         $procentDraw=0;
  50.                                 }else{
  51.                                         //calculate procent draw if is not 0
  52.                                         $procentDraw=round(($rowD['gameDraw']*100)/$rowG['gameP'],2);
  53.                                 }
  54.                                 //display numbers and procent of games finished by draw
  55.                                 $html.='<td>'.$rowD['gameDraw'].' ('.$procentDraw.'%) </td>';
  56.                                
  57.                                 //$GLOBALS['db_in_debug']=1;
  58.                                
  59.                                 //calculate numbers of games lost
  60.                                 $gameLost=$rowG['gameP']-($gamesWon['top_win']+$rowD['gameDraw']);
  61.                                 if($gameLost=='0'){
  62.                                 //if game lost is 0 then the procent of games lost is 0 too (cannot calculate with divide by 0)
  63.                                         $procentLost=0;
  64.                                 }else{
  65.                                         //calculate procent of losts games if is not 0
  66.                                         $procentLost=round(($gameLost*100)/$rowG['gameP'],2);
  67.                                 }
  68.                                 $html.='<td>'.$gameLost.' ('.$procentLost.'%)</td></tr>';
  69.                         }
  70.                 }
  71.         }
  72.                        
  73.                 $html.='</table></br>';
  74.                
  75.                 return $html;
  76. }