-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathitems.php
More file actions
42 lines (36 loc) · 1.04 KB
/
items.php
File metadata and controls
42 lines (36 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?php
/**
* Assignment 4 - Shopping List
*
*/
/**
* this function do conect to database and get all data from shopping_item table
* this code will be included in other php files
*
*/
function Get_Items()
{
include "connect.php"; // adding connect code from connect.php
// preper the select command
// gett all records from shopping_item table
$command = "SELECT id, item, quantity, checked
FROM shopping_item
ORDER BY checked, item";
$stmt = $dbh->prepare($command);
$success = $stmt->execute(); // execute select commsnd
if (!$success) {
header("Location: error.html"); //redirect to error page
}
// item present shopping_item table data
$items = [];
while ($row = $stmt->fetch()) {
array_push($items, [
"id" => $row["id"],
"item" => $row["item"],
"quantity" => (int)$row["quantity"],
"checked" => $row["checked"],
]);
}
// write json encoded array to HTTP response
echo json_encode($items);
}