thieumao

Thong ke dau duoi chuan

Dec 12th, 2013
295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.66 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. if (isset($_GET["loai"])) {
  15.     $loai = $_GET["loai"]; // lo, dac biet, nhat to, hai cua
  16. } else {
  17.     $loai = "lo";
  18. }
  19.  
  20. require_once __DIR__ . '/db_config.php';
  21.  
  22. // Ket noi database de lay du lieu
  23. $conn = mysql_connect(DB_SERVER, DB_USER, DB_PASSWORD) or die("Can't connect this database");
  24. mysql_select_db("appxoso", $conn);
  25. $vung   = str_replace("-", "", $vung);
  26. $sql    = "SELECT * FROM $vung ORDER BY pid DESC LIMIT $limit";
  27. $result = mysql_query($sql);
  28.  
  29. function EchoJson($iStart, $iEnd, $result){
  30.         // echo "Dac biet";
  31.                 // $thongke la luot xuat hien
  32.         for ($i = 0; $i < 100; $i++) {
  33.             $thongke[$i] = 0;
  34.         }
  35.        
  36.         // Lay truong ket qua ra de xu ly
  37.         while ($row = mysql_fetch_array($result)) {
  38.             $ketqua   = $row["ketqua"];
  39.             $ketqua   = str_replace("-", ",", $ketqua);
  40.             $xlKetqua = explode(",", $ketqua);
  41.                      
  42.             for ($i = $iStart; $i <= $iEnd; $i++) {
  43.                 if (substr($xlKetqua[$i], -2, 1) == 0) {
  44.                     $hai2so = substr($xlKetqua[$i], -1);
  45.                 } else {
  46.                     $hai2so = substr($xlKetqua[$i], -2);
  47.                 }
  48.                 $thongke[$hai2so]++;
  49.             }
  50.         }
  51.        
  52.         // Dua ra day so
  53.         $dayso = "";
  54.         for ($i = 0; $i <= 9; $i++) {
  55.             $dayso = $dayso . "0" . $i . ",";
  56.         }
  57.         for ($i = 10; $i <= 98; $i++) {
  58.             $dayso = $dayso . $i . ",";
  59.         }
  60.         $dayso = $dayso . "99";
  61.        
  62.         // Tinh luot xuat hien
  63.         $dayxuathien = "";
  64.         for ($i = 0; $i <= 98; $i++) {
  65.             $dayxuathien = $dayxuathien . $thongke[$i] . ",";
  66.         }
  67.         $dayxuathien = $dayxuathien . $thongke[99];
  68.        
  69.         // Tinh phan tram xuat hien
  70.         $tong = 0;
  71.         for ($i = 0; $i <= 99; $i++) {
  72.             $tong = $tong + $thongke[$i];
  73.         }
  74.         if ($tong==0){
  75.             $tong=1;
  76.         }
  77.         for ($i = 0; $i <= 99; $i++) {
  78.             $phantram[$i] = $thongke[$i] / $tong * 100;
  79.             $phantram[$i] = round($phantram[$i], 2);
  80.         }
  81.         $dayphantram = "";
  82.         for ($i = 0; $i <= 98; $i++) {
  83.             $dayphantram = $dayphantram . $phantram[$i] . ",";
  84.         }
  85.         $dayphantram = $dayphantram . $phantram[99];
  86.        
  87.         // echo $dayso . "<br>";
  88.         // echo $dayxuathien . "<br>";
  89.         // echo $dayphantram . "<br>";
  90.        
  91.         $response["success"] = 1;
  92.         $response["message"] = array();
  93.        
  94.         $kq["dayso"]       = $dayso;
  95.         $kq["dayxuathien"] = $dayxuathien;
  96.         $kq["dayphantram"] = $dayphantram;
  97.         array_push($response["message"], $kq);
  98.        
  99.         echo json_encode($response);
  100. }
  101.  
  102. if ($result) {
  103.     if ($loai == "lo") {
  104.         // $thongke la luot xuat hien
  105.         for ($i = 0; $i < 100; $i++) {
  106.             $thongke[$i] = 0;
  107.         }
  108.        
  109.         // Lay truong ket qua ra de xu ly
  110.         while ($row = mysql_fetch_array($result)) {
  111.             $ketqua   = $row["ketqua"];
  112.             $ketqua   = str_replace("-", ",", $ketqua);
  113.             $xlKetqua = explode(",", $ketqua);
  114.                      
  115.             for ($i = 0; $i < count($xlKetqua); $i++) {
  116.                 if (substr($xlKetqua[$i], -2, 1) == 0) {
  117.                     $hai2so = substr($xlKetqua[$i], -1);
  118.                 } else {
  119.                     $hai2so = substr($xlKetqua[$i], -2);
  120.                 }
  121.                 $thongke[$hai2so]++;
  122.             }
  123.         }
  124.        
  125.         // Dua ra day so
  126.         $dayso = "";
  127.         for ($i = 0; $i <= 9; $i++) {
  128.             $dayso = $dayso . "0" . $i . ",";
  129.         }
  130.         for ($i = 10; $i <= 98; $i++) {
  131.             $dayso = $dayso . $i . ",";
  132.         }
  133.         $dayso = $dayso . "99";
  134.        
  135.         // Tinh luot xuat hien
  136.         $dayxuathien = "";
  137.         for ($i = 0; $i <= 98; $i++) {
  138.             $dayxuathien = $dayxuathien . $thongke[$i] . ",";
  139.         }
  140.         $dayxuathien = $dayxuathien . $thongke[99];
  141.        
  142.         // Tinh phan tram xuat hien
  143.         $tong = 0;
  144.         for ($i = 0; $i <= 99; $i++) {
  145.             $tong = $tong + $thongke[$i];
  146.         }
  147.         for ($i = 0; $i <= 99; $i++) {
  148.             $phantram[$i] = $thongke[$i] / $tong * 100;
  149.             $phantram[$i] = round($phantram[$i], 2);
  150.         }
  151.         $dayphantram = "";
  152.         for ($i = 0; $i <= 98; $i++) {
  153.             $dayphantram = $dayphantram . $phantram[$i] . ",";
  154.         }
  155.         $dayphantram = $dayphantram . $phantram[99];
  156.        
  157.         // echo $dayso . "<br>";
  158.         // echo $dayxuathien . "<br>";
  159.         // echo $dayphantram . "<br>";
  160.        
  161.         $response["success"] = 1;
  162.         $response["message"] = array();
  163.        
  164.         $kq["dayso"]       = $dayso;
  165.         $kq["dayxuathien"] = $dayxuathien;
  166.         $kq["dayphantram"] = $dayphantram;
  167.         array_push($response["message"], $kq);
  168.        
  169.         echo json_encode($response);
  170.     } else if ($loai == "dacbiet") {
  171.         EchoJson(0,0,$result);
  172.     } else if ($loai == "nhatto") {
  173.         EchoJson(1,1,$result);
  174.     } else if ($loai == "haicua") {
  175.         EchoJson(0,1,$result);
  176.     } else {
  177.         $response["success"] = 0;
  178.         $response["message"] = "Error";
  179.         echo json_encode($response);
  180.     }
  181. } else {
  182.     $response["success"] = 0;
  183.     $response["message"] = "Error";
  184.     echo json_encode($response);
  185. }
  186.  
  187. ?>
Advertisement
Add Comment
Please, Sign In to add comment