thieumao

updatetungvung.php

Mar 16th, 2014
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.38 KB | None | 0 0
  1. <?php
  2. /*
  3.  * Gui vung de update
  4.  * vi du: updatetungvung.php?vung=xo-so-mien-bac
  5.  */
  6. include('../simplehtmldom/simple_html_dom.php');
  7.  
  8. function GetDataXSMB($strLink)
  9. {
  10.    
  11.     // -----------------------------------------------------------------------------
  12.    
  13.     // Retrieve the DOM from a given URL
  14.     $html = file_get_html($strLink);
  15.    
  16.     $i          = 0;
  17.     $timeupdate = array();
  18.     foreach ($html->find('span[style="font-size:14px; color:#fff; "]') as $e) {
  19.         $timeupdate[$i++] = $e->innertext;
  20.     }
  21.    
  22.     $i       = 0;
  23.     $product = array();
  24.     foreach ($html->find('td[class="number_kq"]') as $e) {
  25.         $product[$i++] = $e->innertext;
  26.     }
  27.    
  28.     $response["products"] = array();
  29.     for ($i = 4; $i >= 0; $i--) {
  30.         $kqmb["Time"] = $timeupdate[$i];
  31.         //$j=$i*9;
  32.         $j            = $i * 8;
  33.        
  34.         $kqmb["DB"] = $product[$j++];
  35.         $kqmb["G1"] = $product[$j++];
  36.         $kqmb["G2"] = $product[$j++];
  37.         $kqmb["G3"] = $product[$j++];
  38.         $kqmb["G4"] = $product[$j++];
  39.         $kqmb["G5"] = $product[$j++];
  40.         $kqmb["G6"] = $product[$j++];
  41.         $kqmb["G7"] = $product[$j++];
  42.         //$kqmb["G8"]=$product[$j++];
  43.        
  44.         array_push($response["products"], $kqmb);
  45.        
  46.         $ketqua   = $kqmb["DB"] . "," . $kqmb["G1"] . "," . $kqmb["G2"] . "," . $kqmb["G3"] . "," . $kqmb["G4"] . "," . $kqmb["G5"] . "," . $kqmb["G6"] . "," . $kqmb["G7"]; // . "," . $kqmb["G8"];
  47.         $thoigian = $kqmb["Time"];
  48.        
  49.         $thoigian = trim($thoigian);
  50.         $tg       = explode(" ", $thoigian);
  51.         $thoigian = $tg[1];
  52.        
  53.         //echo  $thoigian . "   " . $ketqua . "<br/>";
  54.        
  55.        
  56.         require_once __DIR__ . '/db_config.php';
  57.         $conn = mysql_connect(DB_SERVER, DB_USER, DB_PASSWORD) or die("Can't connect this database");
  58.         mysql_select_db(DB_DATABASE, $conn);
  59.         // mysql inserting a new row
  60.         $ketqua   = str_replace(" ", "", $ketqua);
  61.         $thoigian = str_replace(" ", "", $thoigian);
  62.         if (strlen($thoigian) == 10) {
  63.             $sql   = "SELECT * FROM xosomienbac WHERE thoigian='$thoigian'";
  64.             $query = mysql_query($sql);
  65.             if (mysql_num_rows($query) == 0) {
  66.                 $result = mysql_query("INSERT INTO xosomienbac(ketqua, thoigian) VALUES('$ketqua', '$thoigian')");
  67.                 echo "Da insert $thoigian --> $ketqua <br>";
  68.             } else {
  69.                 $result = mysql_query("UPDATE xosomienbac SET ketqua='$ketqua' WHERE thoigian='$thoigian'");
  70.                 echo "Da update $thoigian --> $ketqua <br>";
  71.             }
  72.         }
  73.        
  74.     }
  75. }
  76.  
  77. function GetData($strLink, $strBang)
  78. {
  79.     // Retrieve the DOM from a given URL
  80.     $html = file_get_html($strLink);
  81.    
  82.     $i          = 0;
  83.     $timeupdate = array();
  84.     foreach ($html->find('span[style="font-size:14px; color:#fff; "]') as $e) {
  85.         $timeupdate[$i++] = $e->innertext;
  86.     }
  87.    
  88.     $i       = 0;
  89.     $product = array();
  90.     foreach ($html->find('td[class="number_kq"]') as $e) {
  91.         $product[$i++] = $e->innertext;
  92.     }
  93.    
  94.     $response["products"] = array();
  95.     for ($i = 4; $i >= 0; $i--) {
  96.         $kqmb["Time"] = $timeupdate[$i];
  97.         $j            = $i * 9;
  98.        
  99.         $kqmb["DB"] = $product[$j++];
  100.         $kqmb["G1"] = $product[$j++];
  101.         $kqmb["G2"] = $product[$j++];
  102.         $kqmb["G3"] = $product[$j++];
  103.         $kqmb["G4"] = $product[$j++];
  104.         $kqmb["G5"] = $product[$j++];
  105.         $kqmb["G6"] = $product[$j++];
  106.         $kqmb["G7"] = $product[$j++];
  107.         $kqmb["G8"] = $product[$j++];
  108.        
  109.         array_push($response["products"], $kqmb);
  110.        
  111.         $ketqua   = $kqmb["DB"] . "," . $kqmb["G1"] . "," . $kqmb["G2"] . "," . $kqmb["G3"] . "," . $kqmb["G4"] . "," . $kqmb["G5"] . "," . $kqmb["G6"] . "," . $kqmb["G7"] . "," . $kqmb["G8"];
  112.         $thoigian = $kqmb["Time"];
  113.        
  114.         $thoigian = trim($thoigian);
  115.         $tg       = explode(" ", $thoigian);
  116.         $thoigian = $tg[1];
  117.        
  118.         require_once __DIR__ . '/db_config.php';
  119.         $conn = mysql_connect(DB_SERVER, DB_USER, DB_PASSWORD) or die("Can't connect this database");
  120.         mysql_select_db(DB_DATABASE, $conn);
  121.         // mysql inserting a new row
  122.         $ketqua   = str_replace(" ", "", $ketqua);
  123.         $thoigian = str_replace(" ", "", $thoigian);
  124.         if (strlen($thoigian) == 10) {
  125.             $sql   = "SELECT * FROM " . $strBang . " WHERE thoigian='$thoigian'";
  126.             $query = mysql_query($sql);
  127.             if (mysql_num_rows($query) == 0) {
  128.                 $result = mysql_query("INSERT INTO $strBang (ketqua, thoigian) VALUES('$ketqua', '$thoigian')");
  129.                 echo "Da insert $thoigian --> $ketqua <br>";
  130.             } else {
  131.                 $result = mysql_query("UPDATE $strBang SET ketqua='$ketqua' WHERE thoigian='$thoigian'");
  132.                 echo "Da update $thoigian --> $ketqua <br>";
  133.             }
  134.         }
  135.  
  136.     }
  137. }
  138.  
  139. if (isset($_GET["vung"])) {
  140.     $vung = $_GET["vung"]; // noi lay ket qua xo so
  141. } else {
  142.     $vung = "xo-so-mien-bac";
  143. }
  144.  
  145. $bang = $vung;
  146. $link = "http://ketquaxoso.24h.com.vn/" . $bang . "/page1";
  147. $bang = str_replace("-", "", $bang);
  148. echo $bang . " - " . $link . "</br>";
  149.  
  150. if ($bang == "xosomienbac") {
  151.     GetDataXSMB($link);
  152. } else {
  153.     GetData($link, $bang);
  154. }
  155.  
  156. echo "Update finish";
  157. ?>
Advertisement
Add Comment
Please, Sign In to add comment