index.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <!-- Required meta tags -->
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  7. <!-- Bootstrap CSS -->
  8. <link rel="stylesheet" href="//stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
  9. <title>Racing Australia</title>
  10. </head>
  11. <body>
  12. <div class="container">
  13. <h1>Racing Australia</h1>
  14. <?php
  15. require_once 'dbconfig.php';
  16. $dsn = "pgsql:host=$host;port=5432;dbname=$db;user=$username;password=$password";
  17. try{
  18. // create a PostgreSQL database connection
  19. $conn = new PDO($dsn);
  20. // display a message if connected to the PostgreSQL successfully
  21. if($conn){
  22. echo "Connected to the <strong>$db</strong> database successfully!";
  23. }
  24. $query = sprintf("SELECT * FROM horses
  25. WHERE race_date >= %s
  26. ORDER BY race_date, venue, race;", $conn->quote(date("Y-m-d")));
  27. $stmt = $conn->query($query);
  28. // $r = $stmt->fetch(PDO::FETCH_ASSOC);
  29. $table_head = <<<TABLEHEAD
  30. <table class="table table-striped table-bordered">
  31. <thead>
  32. <tr>
  33. <th>id</th>
  34. <th>venue</th>
  35. <th>race_date</th>
  36. <th>race</th>
  37. <th>horse</th>
  38. </tr>
  39. </thead>
  40. <tbody>
  41. TABLEHEAD;
  42. $table_foot = <<<TABLEFOOT
  43. </tbody>
  44. </table>
  45. TABLEFOOT;
  46. echo( $table_head );
  47. $new_venue = '';
  48. $new_day = '';
  49. while($row = $stmt->fetch(PDO::FETCH_ASSOC)) : ?>
  50. <?php
  51. if ( $new_day != '' ) {
  52. if ($new_day != $row['race_date']) {
  53. echo $table_foot;
  54. echo '<h4>' . htmlspecialchars($row['race_date']) . '</h4>';
  55. echo $table_head;
  56. $new_day = $row['race_date'];
  57. }
  58. } else {
  59. $new_day = $row['race_date'];
  60. }
  61. if ( $new_venue != '' ) {
  62. if ($new_venue != $row['venue'] ) {
  63. echo $table_foot;
  64. echo $table_head;
  65. $new_venue = $row['venue'];
  66. }
  67. } else {
  68. $new_venue = $row['venue'];
  69. }
  70. ?>
  71. <tr>
  72. <td><?php echo htmlspecialchars($row['id']); ?></td>
  73. <td><?php echo htmlspecialchars($row['venue']); ?></td>
  74. <td><?php echo htmlspecialchars($row['race_date']); ?></td>
  75. <td><?php echo htmlspecialchars($row['race']); ?></td>
  76. <td><?php echo htmlspecialchars($row['horse']); ?></td>
  77. </tr>
  78. <?php endwhile; ?>
  79. <?php
  80. echo $table_foot;
  81. }catch (PDOException $e){
  82. // report error message
  83. echo $e->getMessage();
  84. }
  85. ?>
  86. </div>
  87. <!-- Optional JavaScript -->
  88. <!-- jQuery first, then Popper.js, then Bootstrap JS -->
  89. <script src="//code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
  90. <script src="//cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
  91. <script src="//stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
  92. </body>
  93. </html>