Advertisement
Guest User

category.php (endpoint)

a guest
Feb 19th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.49 KB | None | 0 0
  1. <?php
  2.  
  3. // required headers
  4. header("Access-Control-Allow-Origin: *");
  5. header("Access-Control-Allow-Credentials: true");
  6. header("Access-Control-Max-Age: 1000");
  7. header("Access-Control-Allow-Headers: X-Requested-With, Content-Type, Origin, Cache-Control, Pragma, Authorization, Accept, Accept-Encoding");
  8. header("Access-Control-Allow-Methods: PUT, POST, GET, OPTIONS, DELETE");
  9. header("Content-Type: application/json; charset=UTF-8");
  10.  
  11. // database connection will be here
  12. include_once '../database.inc';
  13. include_once '../models/Category.php';
  14.  
  15. if ($_SERVER['REQUEST_METHOD'] === 'GET') {
  16.     $id = $_GET['id'];
  17.  
  18.     $database = new Database();
  19.     $db = $database->getConnection();
  20.  
  21.     $Category = new Category($db);
  22.     // query products
  23.     $stmt = $Category->read($id);
  24.     $num = $stmt->rowCount();
  25.    
  26.     // check if more than 0 record found
  27.     if($num>0){
  28.  
  29.         $products_arr=array();
  30.         $products_arr["categories"]=array();
  31.    
  32.         while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
  33.             extract($row);
  34.            
  35.             $product_item=array(
  36.                 "ID" => $ID,
  37.                 "katNavn" => $katNavn,
  38.             );
  39.    
  40.             array_push($products_arr["categories"], $product_item);
  41.         }
  42.    
  43.         // set response code - 200 OK
  44.         http_response_code(200);
  45.    
  46.         // show products data in json format
  47.         echo json_encode($products_arr);
  48.     }else{
  49.    
  50.         // set response code - 404 Not found
  51.         http_response_code(404);
  52.    
  53.         // tell the user no products found
  54.         echo json_encode(
  55.             array("message" => "Ingen kategori fundet")
  56.         );
  57.     }
  58. } else if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  59.    
  60.     $database = new Database();
  61.     $db = $database->getConnection();
  62.  
  63.     $Category = new Category($db);
  64.     // query products
  65.     $stmt = $Category->write($_POST);
  66.     if($stmt === true) {
  67.  
  68.         http_response_code(200);
  69.         echo json_encode(
  70.             array("message" => "Indsatte Kategori i databasen", "result" => 1)
  71.         );
  72.  
  73.     } else {
  74.         // set response code - 404 Not found
  75.         http_response_code(200);
  76.    
  77.         // tell the user no products found
  78.         echo json_encode(
  79.             array("message" => "Kunne ikke Kategori i databasen",
  80.             "result" => 0,
  81.             "error" => $stmt->getMessage(),
  82.             "errMessage" => `Kategorien `.$_POST['katNavn'].` findes allerede i databasen`)
  83.         );
  84.     }
  85. }
  86. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement