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

Untitled

By: a guest on Aug 1st, 2012  |  syntax: PHP  |  size: 4.04 KB  |  hits: 12  |  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. <?php
  2. include('lib/config.php');
  3. session_name($GLOBALS['session_name']);
  4. session_start();
  5.  
  6. include('lib/utils.php');
  7. include('lib/site.inc.php');
  8. include('lib/init.inc.php');
  9.  
  10.  
  11.  
  12. // load the templates
  13. $variables = array();
  14. $page_template = implode('',file('templates/template.html'));
  15. $variables['title'] = $GLOBALS['website_name'];  
  16. $variables['banner'] = $GLOBALS['banner'];
  17.  
  18. $variables['history'] = games_history($conn);
  19.  
  20. $variables['init_game'] = init_game($conn);  
  21.  
  22. $variables['right_info']=statistics($conn);;
  23.  
  24. $variables['footer'] = $GLOBALS['website_footer'];
  25.  
  26. outputHTML($variables,$page_template);
  27.  
  28.  
  29. //display a table with top 10 players and their results
  30. function statistics($conn){
  31.         //count the number of games winned by every winner and order desc
  32.         $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);
  33.         $gamesWonNum=mysql_num_rows($resCount);
  34.         // if there is some value then initiate the table then nothing is display
  35.         if ($gamesWonNum != 0) {
  36.                 $html='<p class="table_title">Statistics </p>
  37.                         <table  class="table_game">
  38.                                         <tr class="x">
  39.                                                 <th class="table_header_games">Name</th>
  40.                                                 <th class="table_header_games">Games played</th>
  41.                                                 <th class="table_header_games">Win</th>
  42.                                                 <th class="table_header_games">Draw</th>
  43.                                                 <th class="table_header_games">Lost</th>
  44.                                         </tr>';
  45.                 //$table_row_class  - diferent class for odd and even rows             
  46.                 $table_row_class = "table_odd_row_games";                      
  47.                 while ($gamesWon=mysql_fetch_assoc($resCount)){
  48.                         //change the class for even and odd rows
  49.                         $table_row_class = ($table_row_class=="table_odd_row_games")?"table_even_row_games":"table_odd_row_games";
  50.                         //select name for every winner form top 10
  51.                         $resN=db_query("SELECT name FROM users WHERE user_id='".$gamesWon['winner_id']."' AND name != 'Draw'",$conn);
  52.                         $rowN = mysql_fetch_assoc($resN);
  53.                         //display name
  54.                         $html.='<tr class="'.$table_row_class.'"><td>'.$rowN['name'].'</td>';
  55.                         //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)                                      
  56.                         $resG=db_query("SELECT SUM(c) AS gameP FROM(   
  57.                                                                                                 SELECT COUNT(user_id_1) AS c FROM games AS games WHERE user_id_1=".$gamesWon['winner_id']."
  58.                                                                                                 UNION
  59.                                                                                                 SELECT COUNT(user_id_2) AS c FROM games AS games WHERE user_id_2=".$gamesWon['winner_id']."
  60.                                                                                                 ) AS total",$conn);
  61.                                                                                                
  62.                         while ($rowG=mysql_fetch_assoc($resG)){
  63.                                 //display numbers of games played
  64.                                 $html.='<td>'.$rowG['gameP'].'</td>';
  65.                                
  66.                                 //calculate procent of win
  67.                                 $procentWin=round(($gamesWon['top_win']*100)/$rowG['gameP'],2);
  68.                                 //display numbers and procent of games winned  
  69.                                 $html.='<td>'.$gamesWon['top_win'].' ('.$procentWin.'%) </td>';
  70.                                 //count the numbers of games finished as draw                                                  
  71.                                
  72.                                 $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);         
  73.                                 $rowD=mysql_fetch_assoc($resD);
  74.                                 if ($rowD['gameDraw']=='0'){
  75.                                         //if game draw is 0 then the procent draw is 0 too (cannot calculate with divide by 0)
  76.                                         $procentDraw=0;
  77.                                 }else{
  78.                                         //calculate procent draw if is not 0
  79.                                         $procentDraw=round(($rowD['gameDraw']*100)/$rowG['gameP'],2);
  80.                                 }
  81.                                 //display numbers and procent of games finished by draw
  82.                                 $html.='<td>'.$rowD['gameDraw'].' ('.$procentDraw.'%) </td>';
  83.                                
  84.                                 //$GLOBALS['db_in_debug']=1;
  85.                                
  86.                                 //calculate numbers of games lost
  87.                                 $gameLost=$rowG['gameP']-($gamesWon['top_win']+$rowD['gameDraw']);
  88.                                 if($gameLost=='0'){
  89.                                 //if game lost is 0 then the procent of games lost is 0 too (cannot calculate with divide by 0)
  90.                                         $procentLost=0;
  91.                                 }else{
  92.                                         //calculate procent of losts games if is not 0
  93.                                         $procentLost=round(($gameLost*100)/$rowG['gameP'],2);
  94.                                 }
  95.                                 $html.='<td>'.$gameLost.' ('.$procentLost.'%)</td></tr>';
  96.                         }
  97.                 }
  98.         }
  99.                        
  100.                 $html.='</table></br>';
  101.                
  102.                 return $html;
  103. }
  104. ?>