Browse Source

Simplify Slim fortunes (#4600)

Joan Miquel 6 years ago
parent
commit
94c2ca7e52
2 changed files with 7 additions and 11 deletions
  1. 4 8
      frameworks/PHP/slim/index.php
  2. 3 3
      frameworks/PHP/slim/templates/fortunes.php

+ 4 - 8
frameworks/PHP/slim/index.php

@@ -115,14 +115,10 @@ $app->get('/updates', function ($request, $response) {
 
 // Test 6: Fortunes
 $app->get('/fortunes', function ($request, $response) {
-    $sth = $this->db->prepare('SELECT * FROM Fortune');
-    $sth->execute();
-    $fortunes = $sth->fetchAll();
-
-    array_push($fortunes, array('id'=> 0, 'message' => 'Additional fortune added at request time.'));
-    usort($fortunes, function($left, $right) {
-        return strcmp($left['message'], $right['message']);
-    });
+    $fortunes = $this->db->query('SELECT * FROM Fortune')->fetchAll(PDO::FETCH_KEY_PAIR);
+
+    $fortunes[0] = 'Additional fortune added at request time.';
+    asort($fortunes);
 
     return $this->view->render($response, "fortunes.php", ["fortunes" => $fortunes]);
 });

+ 3 - 3
frameworks/PHP/slim/templates/fortunes.php

@@ -7,10 +7,10 @@
             <th>id</th>
             <th>message</th>
         </tr>
-        <?php foreach($data['fortunes'] as $fortune){ ?>
+        <?php foreach($data['fortunes'] as $id =>$fortune){ ?>
             <tr>
-                <td><?php echo $fortune['id']; ?></td>
-                <td><?php echo htmlspecialchars($fortune['message']); ?></td>
+                <td><?php echo $id; ?></td>
+                <td><?php echo htmlspecialchars($fortune); ?></td>
             </tr>
         <?php } ?>
     </table>