thieumao

Thong ke dau duoi

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