Advertisement
EndymionSpr

Untitled

Jan 13th, 2021
942
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.14 KB | None | 0 0
  1. <?php
  2.   // Headers
  3.   header('Access-Control-Allow-Origin: *');
  4.   header('Content-Type: application/json');
  5.  
  6.   include_once '../../config/Database.php';
  7.   include_once '../../models/Forum.php';
  8.  
  9.   // Instantiate DB & connect
  10.   $database = new Database();
  11.   $db = $database->connect();
  12.  
  13.   // Instantiate forum object
  14.   $forum = new Forum($db);
  15.  
  16.   // forum read query
  17.   $result = $forum->read();
  18.  
  19.   // Get row count
  20.   $num = $result->rowCount();
  21.  
  22.   // Check if any forum
  23.   if($num > 0) {
  24.         // forum array
  25.         $pla_arr = array();
  26.         $pla_arr['data'] = array();
  27.  
  28.         while($row = $result->fetch(PDO::FETCH_ASSOC)) {
  29.           extract($row);
  30.  
  31.           $pla_item = array(
  32.             'id' => $id,
  33.             'nickname' => $plant_name,
  34.             'title' => $plant_type,
  35.             'description' => $details
  36.           );
  37.  
  38.           // Push to "data"
  39.           array_push($pla_arr['data'], $pla_item);
  40.         }
  41.  
  42.         // Turn to JSON & output
  43.         echo json_encode($pla_arr);
  44.  
  45.   } else {
  46.         // No forum
  47.         echo json_encode(
  48.           array('message' => 'No forum Found')
  49.         );
  50.   }
  51.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement