Parcourir la source

Uploaded API "read" PHP file which processes GET requests for inventory data

The "read.php" file instantiates the Database and Inventorio classes and processes the SQL query with which the Inventorio object retrieves all the names of the products and their respective sellers from the database. This information is then outputted as a JSON file.
miguel.cruz15 il y a 2 ans
Parent
révision
4024b689fe
1 fichiers modifiés avec 56 ajouts et 0 suppressions
  1. 56
    0
      api/read.php

+ 56
- 0
api/read.php Voir le fichier

@@ -0,0 +1,56 @@
1
+<?php
2
+
3
+    // Headers:
4
+
5
+    header('Access-Control-Allow-Origin: *');
6
+    header('Content-Type: application/json');
7
+
8
+    include_once '../../config/Database.php';
9
+    include_once '../../models/Inventario.php';
10
+
11
+    // Conectarse a la base de datos:
12
+
13
+    $database = new Database();
14
+    $db = $database->connect();
15
+
16
+    // Crear instancia de inventario para obtener productos y comerciantes:
17
+
18
+    $inventario = new Inventario($db);
19
+
20
+    // Obtener datos del inventario:
21
+
22
+    $datos = $inventario->read();
23
+
24
+    // Contar filas de datos obtenidos:
25
+
26
+    $num = $datos->rowCount();
27
+
28
+    // Si la tabla solicitada no está vacía...
29
+
30
+    if ($num > 0) {
31
+
32
+      // Crear arreglo de datos:
33
+
34
+      $inventario_arr = array();
35
+      $inventario_arr['datos'] = array();
36
+
37
+      while($row = $datos->fetch(PDO::FETCH_ASSOC)) {
38
+        extract($row);
39
+
40
+        $producto = array(
41
+          'producto' => $nom_producto,
42
+          'comerciante' => $nom_comerciante
43
+        );
44
+
45
+        array_push($inventario_arr['datos'], $producto);
46
+      }
47
+
48
+      // Convertir a JSON y desplegar:
49
+
50
+      echo \json_enconde($inventario_arr);
51
+
52
+    } else {
53
+      echo \json_enconde(array('message' => 'No hay productos disponibles.'));
54
+    }
55
+
56
+?>