SuperMonkey

getScores

Aug 12th, 2012
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.53 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * renvoie un array du style
  5.  * "hordes" => 582
  6.  * "odyssey" => 200
  7.  * "fever" => 40
  8.  * et ce qui valent 0 ne sont pas dans l'array.
  9.  */
  10.  
  11. function getScores($uid) {
  12.     $html = file_get_contents("http://www.twinoid.com/fr/user/".$uid);
  13.  
  14.     $out = array();
  15.  
  16.     if(preg_match_all('#<div class="tid_score">([0-9]+)</div>#', $html, $scores)) {
  17.         $n = count($scores[1]);
  18.         preg_match_all('#<div class="tid_name">(.*)</div>#', $html, $jeux);
  19.         for($i = 0; $i < $n; $i++) {
  20.             $out[$jeux[1][$i]] = $scores[1][$i];
  21.         }
  22.     }
  23.        
  24.     return $out;
  25. }
  26.  
  27. //petite fonction pour faire le total, pour le petit exemple qui suit :) (tu devrais comprendre facilement)
  28. function somme($a) {
  29.     $somme = 0;
  30.     foreach($a as $val)
  31.         $somme += $val;
  32.     return $somme;
  33. }
  34.  
  35. function moyenne($a) {
  36.     $moyenne = 0;
  37.     if(count($a) > 0) {
  38.         foreach($a as $val)
  39.             $moyenne += $val;
  40.         $moyenne /= count($a);
  41.     }
  42.     return $moyenne;
  43. }
  44.  
  45. //un petit exemple de code pour afficher dans un tableau les données
  46. echo "<table>";
  47.     echo "<tr><td><strong>Nom du jeu</strong></td><td><strong>Score du joueur</strong></td></tr>";
  48.     $scores = getScores($_GET["uid"]);
  49.     foreach($scores as $jeu => $score) {
  50.         echo "<tr><td><span style='font-weight:bold;'>".$jeu."</span></td><td>".$score."</td></tr>";
  51.     }
  52.     echo "<tr><td><strong>Somme</strong></td><td>".somme($scores)."</td></tr>";
  53.     echo "<tr><td><strong>Moyenne</strong></td><td>".moyenne($scores)."</td></tr>";
  54. echo "</table>";
  55. ?>
Advertisement
Add Comment
Please, Sign In to add comment