video_games.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. declare(strict_types=1);
  3. $postsDir = __DIR__ . '/posts/';
  4. $posts = [];
  5. // scan markdown files
  6. foreach (glob($postsDir . '*.md') as $file) {
  7. $filename = basename($file, '.md');
  8. $content = file_get_contents($file);
  9. // extract first markdown H1
  10. if (preg_match('/^#\s+(.+)$/m', $content, $matches)) {
  11. $title = trim($matches[1]);
  12. } else {
  13. $title = $filename;
  14. }
  15. $posts[] = [
  16. 'slug' => $filename,
  17. 'title' => $title,
  18. 'time' => filemtime($file),
  19. ];
  20. }
  21. // sort newest first
  22. usort($posts, fn($a, $b) => $b['time'] <=> $a['time']);
  23. ?>
  24. <!DOCTYPE html>
  25. <html lang="pl">
  26. <head>
  27. <meta charset="UTF-8">
  28. <meta name="viewport" content="width=device-width, initial-scale=1">
  29. <title>Blog</title>
  30. <link rel="stylesheet" href="../style.css">
  31. <script src="https://unpkg.com/cursor-effects@latest/dist/browser.js"></script>
  32. <script>
  33. window.addEventListener("load", (event) => {
  34. new cursoreffects.ghostCursor();
  35. });
  36. </script>
  37. </head>
  38. <body>
  39. <div class="container bio">
  40. <h1>Video Game list</h1>
  41. <p>I have a SQLite database where I store games that I have played. This database contains title of the game, platform on which I have played or emulated the game, my grade and short description of what do I think of the game. Grading system works like this: 5 means mixed feelings, 1 worst games, 10 best games. Happy reading :3</p>
  42. <form method="POST" action="">
  43. <input type="text" name="search" placeholder="Search by game name" style="margin-bottom: 10px; padding: 8px; width: calc(100% - 16px);"/>
  44. <select name="sort" style="padding: 8px; margin-bottom: 10px;">
  45. <option value="">Sort By</option>
  46. <option value="name_asc">Name (A-Z)</option>
  47. <option value="name_desc">Name (Z-A)</option>
  48. <option value="grade_asc">Grade (Lowest to Highest)</option>
  49. <option value="grade_desc">Grade (Highest to Lowest)</option>
  50. </select>
  51. <input type="submit" value="Apply" style="padding: 8px;"/>
  52. </form>
  53. <table border="1" style="width: 100%; border-collapse: collapse;" id="gameTable">
  54. <thead>
  55. <tr>
  56. <th>Game Name</th>
  57. <th>Platform</th>
  58. <th>Grade</th>
  59. <th>Description</th>
  60. </tr>
  61. </thead>
  62. <tbody>
  63. <?php
  64. // Establish a database connection
  65. $db = new SQLite3('/ostrowski.net.pl/games.db');
  66. // Initialize variables for search and sorting
  67. $search = isset($_POST['search']) ? $_POST['search'] : '';
  68. $sort = isset($_POST['sort']) ? $_POST['sort'] : '';
  69. // Building query based on search and sort
  70. $query = "SELECT * FROM games WHERE name LIKE :search";
  71. if ($sort === 'name_asc') {
  72. $query .= " ORDER BY name ASC";
  73. } elseif ($sort === 'name_desc') {
  74. $query .= " ORDER BY name DESC";
  75. } elseif ($sort === 'grade_asc') {
  76. $query .= " ORDER BY grade ASC";
  77. } elseif ($sort === 'grade_desc') {
  78. $query .= " ORDER BY grade DESC";
  79. }
  80. $stmt = $db->prepare($query);
  81. $stmt->bindValue(':search', '%' . $search . '%', SQLITE3_TEXT);
  82. // Execute the query
  83. $results = $stmt->execute();
  84. // Loop through the results and display them in the table
  85. while ($row = $results->fetchArray(SQLITE3_ASSOC)) {
  86. echo "<tr>";
  87. echo "<td>{$row['name']}</td>";
  88. echo "<td>{$row['platform']}</td>";
  89. echo "<td>{$row['grade']}</td>";
  90. echo "<td>{$row['description']}</td>";
  91. echo "</tr>";
  92. }
  93. ?>
  94. </tbody>
  95. </table>
  96. </div>
  97. </main>
  98. <footer>
  99. <p class="center"><img src="../favicon.ico">(c) computer_glamour</p>
  100. <img src="../buttons/-18.gif" class="footer-button">
  101. <img src="../buttons/xmpp.gif" class="footer-button">
  102. <img src="../buttons/notread.gif" class="footer-button">
  103. <img src="../buttons/right2repair.gif" class="footer-button">
  104. </footer>
  105. </body>
  106. </html>