Advertisement
Guest User

Untitled

a guest
Dec 15th, 2016
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.48 KB | None | 0 0
  1. <?php
  2. $servername = "localhost";
  3. $username = "uname";
  4. $password = "pwd";
  5. $dbname = "alerts";
  6.  
  7. $conn = new mysqli($servername, $username, $password, $dbname);
  8.  
  9. // Check connection
  10.  
  11. if ($conn->connect_error)
  12.     {
  13.     die("Connection failed: " . $conn->connect_error);
  14.     }
  15.    
  16.  
  17. date_default_timezone_set("America/Toronto");
  18. $hours = date(g);
  19. $minute = date(i);
  20. $ante= date(A);
  21. $time = "[$hours:$minute$ante]";
  22. $url = "https://weather.gc.ca/rss/warning/on-54_e.xml";
  23. $data= file_get_contents($url);
  24. $xml= simplexml_load_string($data);
  25. $json = json_encode($xml);
  26. $array = json_decode($json,TRUE);
  27.  
  28.  
  29. if ($array['entry']['title'] !== "No watches or warnings in effect, Pickering") {
  30. if ($array['entry']['title'] !== null) {
  31.     $id0 = $array['entry']['id'];
  32.     $sql = "SELECT id FROM alerts WHERE headline LIKE '$id0';";
  33.     $result = $conn->query($sql);
  34.     if ($result->num_rows == 0) {
  35.     $flag0 = true;
  36.     }
  37.     $alert0string = $array['entry']['title'];
  38.     $newTitle0 = str_replace(", Pickering", " for Ajax", $alert0string);
  39.     $alert0desc = $array['entry']['summary'];
  40.     preg_match("@\d{1,2}:\d{2} (AM|PM)@", $alert0desc, $matches0);
  41.     $alert0time = $matches0[0];
  42.     $alert0 = ($newTitle0.' at '.$alert0time);
  43.     $alertfile = $alert0;
  44.        
  45.    
  46. } elseif ($array['entry'][0]['title'] !== null) {
  47.     $id0 = $array['entry'][0]['id'];
  48.     $sql = "SELECT id FROM alerts WHERE headline LIKE '$id0';";
  49.     $result = $conn->query($sql);
  50.     if ($result->num_rows == 0) {
  51.     $flag0 = true;
  52.     }
  53.     $alert0string = $array['entry'][0]['title'];
  54.     $newTitle0 = str_replace(", Pickering", " for Ajax", $alert0string);
  55.     $alert0desc = $array['entry'][0]['summary'];
  56.     preg_match("@\d{1,2}:\d{2} (AM|PM)@", $alert0desc, $matches0);
  57.     $alert0time = $matches0[0];
  58.     $alert0 = ($newTitle0.' at '.$alert0time);
  59.     $alertfile = $alert0;
  60. }
  61. if ($array['entry'][1]['title'] !== null) {
  62.     $id1 = $array['entry'][1]['id'];
  63.     $sqla = "SELECT id FROM alerts WHERE headline LIKE '$id1';";
  64.     $resulta = $conn->query($sqla);
  65.     if ($resulta->num_rows == 0) {
  66.     $flag1 = true;
  67.     }
  68.     $alert1string = $array['entry'][1]['title'];
  69.     $newTitle1 = str_replace(", Pickering", " for Ajax", $alert1string);
  70.     $alert1desc = $array['entry'][1]['summary'];
  71.     preg_match("@\d{1,2}:\d{2} (AM|PM)@", $alert1desc, $matches1);
  72.     $alert1time = $matches1[0];
  73.     $alert1 = ($newTitle1.' at '.$alert1time);
  74.     $alertfile = ($alert0.$alert1);
  75.    
  76. }
  77. if ($array['entry'][2]['title'] !== null) {
  78.     $id2 = $array['entry'][2]['id'];
  79.     $sqlb = "SELECT id FROM alerts WHERE headline LIKE '$id2';";
  80.     $resultb = $conn->query($sqlb);
  81.     if ($resultb->num_rows == 0) {
  82.     $flag2 = true;
  83.     }
  84.     $alert2string = $array['entry'][2]['title'];
  85.     $newTitle2 = str_replace(", Pickering", " for Ajax", $alert2string);
  86.     $alert2desc = $array['entry'][2]['summary'];
  87.     preg_match("@\d{1,2}:\d{2} (AM|PM)@", $alert2desc, $matches2);
  88.     $alert2time = $matches2[0];
  89.     $alert2 = ($newTitle2.' at '.$alert2time);
  90.     $alertfile = ($alert0.$alert1.$alert2);
  91.    
  92. }
  93. } else {
  94. $alertfile = $array['entry']['title'];
  95. }
  96.  
  97.  
  98.     if ($alert0 !== null && $flag0 == true) {
  99.             echo $alert0;
  100.  
  101.            
  102.             $sqlc = "INSERT INTO alerts (headline) VALUES ('$id0')";
  103.             $resultc = $conn->query($sqlc);
  104.         }
  105.         if ($alert1 !== null && $flag1 == true) {      
  106.             echo $alert1;
  107.            
  108.             $sqld = "INSERT INTO alerts (headline) VALUES ('$id1')";
  109.             $resultd = $conn->query($sqld);
  110.         }
  111.         if ($alert2 !== null && $flag2 == true) {      
  112.             echo $alert2;
  113.            
  114.             $sqle = "INSERT INTO alerts (headline) VALUES ('$id2')";
  115.             $resulte = $conn->query($sqle);
  116.         }
  117.    
  118.  
  119. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement