thieumao

Thống kê theo lô

Dec 9th, 2013
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.82 KB | None | 0 0
  1. <?php
  2. if (isset($_GET["vung"])) {
  3.     $vung = $_GET["vung"]; // noi lay ket qua xo so
  4. } else {
  5.     $vung = "xo-so-mien-bac";
  6. }
  7.  
  8. if (isset($_GET["limit"])) {
  9.     $limit = $_GET["limit"]; // so ngay cach day
  10. } else {
  11.     $limit = 1;
  12. }
  13.  
  14. require_once __DIR__ . '/db_config.php';
  15.  
  16. // Ket noi database de lay du lieu
  17. $conn = mysql_connect(DB_SERVER, DB_USER, DB_PASSWORD) or die("Can't connect this database");
  18. mysql_select_db("appxoso", $conn);
  19. $vung   = str_replace("-", "", $vung);
  20. $sql    = "SELECT * FROM $vung ORDER BY pid DESC LIMIT $limit";
  21. $result = mysql_query($sql);
  22.  
  23. if ($result) {
  24.     // $thongke la luot xuat hien
  25.     for ($i = 0; $i < 100; $i++) {
  26.         $thongke[$i] = 0;
  27.     }
  28.    
  29.     // Lay truong ket qua ra de xu ly
  30.     while ($row = mysql_fetch_array($result)) {
  31.         $ketqua   = $row["ketqua"];
  32.         $ketqua   = str_replace("-", ",", $ketqua);
  33.         $xlKetqua = explode(",", $ketqua);
  34.        
  35.         // for ($i = 0; $i < count($xlKetqua); $i++) {
  36.         //     echo substr($xlKetqua[$i], -2) . "-";
  37.         //     // if ($i % 10 == 0) echo "<br>";
  38.         // }
  39.         // echo "<br><br><br>";
  40.        
  41.         for ($i = 0; $i < count($xlKetqua); $i++) {
  42.             if (substr($xlKetqua[$i], -2, 1) == 0) {
  43.                 $hai2so = substr($xlKetqua[$i], -1);
  44.             } else {
  45.                 $hai2so = substr($xlKetqua[$i], -2);
  46.             }
  47.             $thongke[$hai2so]++;
  48.         }
  49.     }
  50.    
  51.     // Dua ra day so
  52.     $dayso = "";
  53.     for ($i = 0; $i <= 9; $i++) {
  54.         $dayso = $dayso . "0" . $i . ",";
  55.     }
  56.     for ($i = 10; $i <= 98; $i++) {
  57.         $dayso = $dayso . $i . ",";
  58.     }
  59.     $dayso = $dayso . "99";
  60.    
  61.     // Tinh luot xuat hien
  62.     $dayxuathien = "";
  63.     for ($i = 0; $i <= 98; $i++) {
  64.         $dayxuathien = $dayxuathien . $thongke[$i] . ",";
  65.     }
  66.     $dayxuathien = $dayxuathien . $thongke[99];
  67.    
  68.     // Tinh phan tram xuat hien
  69.     $tong = 0;
  70.     for ($i = 0; $i <= 99; $i++) {
  71.         $tong = $tong + $thongke[$i];
  72.     }
  73.     for ($i = 0; $i <= 99; $i++) {
  74.         $phantram[$i] = $thongke[$i] / $tong * 100;
  75.         $phantram[$i] = round($phantram[$i], 2);
  76.     }
  77.     $dayphantram = "";
  78.     for ($i = 0; $i <= 98; $i++) {
  79.         $dayphantram = $dayphantram . $phantram[$i] . ",";
  80.     }
  81.     $dayphantram = $dayphantram . $phantram[99];
  82.    
  83.     // echo $dayso . "<br>";
  84.     // echo $dayxuathien . "<br>";
  85.     // echo $dayphantram . "<br>";
  86.    
  87.     $response["success"] = 1;
  88.     $response["message"] = array();
  89.    
  90.     $kq["dayso"]       = $dayso;
  91.     $kq["dayxuathien"] = $dayxuathien;
  92.     $kq["dayphantram"] = $dayphantram;
  93.     array_push($response["message"], $kq);
  94.    
  95.     echo json_encode($response);
  96. } else {
  97.     $response["success"] = 0;
  98.     $response["message"] = "Error";
  99.     echo json_encode($response);
  100. }
  101.  
  102. ?>
Advertisement
Add Comment
Please, Sign In to add comment