euromillions.php 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. $available_formats = array(
  3. 'txt',
  4. 'xml',
  5. 'json'
  6. );
  7. $available_formats = array_fill_keys($available_formats, 1);
  8. $f = $_REQUEST["format"];
  9. if (!$available_formats[$f]) {
  10. $f = "txt";
  11. }
  12. $q = trim($_REQUEST['result']);
  13. if (!preg_match('/^[a-z0-9 .\-]+$/i', $q)) {
  14. $q = "";
  15. }
  16. try {
  17. $sqlite = new PDO('sqlite:em.sqlite');
  18. }
  19. catch (PDOException $e) {
  20. echo 'Connection failed: ' . $e->getMessage();
  21. }
  22. if ($q == "") {
  23. $statement = $sqlite->prepare("SELECT date,ball_1,ball_2,ball_3,ball_4,ball_5,star_1,star_2 FROM euro_millions ORDER BY date DESC limit 1");
  24. } elseif ($q == "all") {
  25. $statement = $sqlite->prepare("SELECT date,ball_1,ball_2,ball_3,ball_4,ball_5,star_1,star_2 FROM euro_millions ORDER BY date DESC");
  26. } else {
  27. $statement = $sqlite->prepare("SELECT date,ball_1,ball_2,ball_3,ball_4,ball_5,star_1,star_2 FROM euro_millions WHERE date LIKE '%$q%' ORDER BY date DESC");
  28. }
  29. try {
  30. $statement->execute();
  31. }
  32. catch (PDOException $e) {
  33. echo "Statement failed: " . $e->getMessage();
  34. return false;
  35. }
  36. $result = $statement->fetchAll();
  37. $numResults = count($result);
  38. if ($f == "xml") {
  39. header('Content-type: text/xml');
  40. echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
  41. echo ' <euromillions>'. "\n";
  42. foreach ($result as $row) {
  43. echo " <drawn>
  44. " . " <date>" . $row['date'] . "</date>
  45. " . " <balls>" . $row['ball_1'] . " " . $row['ball_2'] . " " . $row['ball_3'] . " " . $row['ball_4'] . " " . $row['ball_5'] . "</balls>
  46. " . " <stars>" . $row['star_1'] . " " . $row['star_2'] . "</stars>
  47. " . " </drawn>" . "\n";
  48. }
  49. echo '</euromillions>';
  50. } elseif ($f == "json") {
  51. header('Content-type: application/json');
  52. print "{\r\n" . ' "drawns": [' . "\r\n";
  53. print "\r\n";
  54. $counter = 0;
  55. foreach ($result as $row) {
  56. if (++$counter == $numResults) {
  57. print " {\r\n" . "\r\n " . '"date":"' . $row['date'] . '", ' . "\r\n " . '"balls":"' . $row['ball_1'] . " " . $row['ball_2'] . " " . $row['ball_3'] . " " . $row['ball_4'] . " " . $row['ball_5'] . '", ' . "\r\n " . '"stars":"' . $row['star_1'] . " " . $row['star_2'] . '"' . "\r\n\r\n }";
  58. print "\r\n";
  59. } else {
  60. print " {\r\n" . "\r\n " . '"date":"' . $row['date'] . '", ' . "\r\n\r\n " . '"balls":"' . $row['ball_1'] . " " . $row['ball_2'] . " " . $row['ball_3'] . " " . $row['ball_4'] . " " . $row['ball_5'] . '", ' . "\r\n\r\n " . '"stars":"' . $row['star_1'] . " " . $row['star_2'] . '"' . "\r\n\r\n },\r\n\r\n";
  61. print "\r\n";
  62. }
  63. }
  64. print "\r\n ]\r\n}";
  65. } else {
  66. header('Content-type: text/plain');
  67. foreach ($result as $row) {
  68. echo $row['date'] . " = " . $row['ball_1'] . " - " . $row['ball_2'] . " - " . $row['ball_3'] . " - " . $row['ball_4'] . " - " . $row['ball_5'] . " + " . $row['star_1'] . " - " . $row['star_2'];
  69. echo "\n";
  70. }
  71. }
  72. ?>