-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_stock.php
115 lines (99 loc) · 2.9 KB
/
update_stock.php
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php //kitchen.php
require_once 'library.php';
require_once 'ordering.php';
require_once 'login.php';
$conn = new mysqli($hn, $un, $pw, $db);
if ($conn->connect_error) mysqli_ferror($conn);
if( isset($_COOKIE['user'])) $name = $_COOKIE['user'];
// Get number of menu items
$query = "SELECT * FROM menuniv";
$result = $conn->query($query);
if (!$result) echo "Couldn't get the menu: " . $conn->error;
$num_items_total = $result->num_rows;
$query = "SELECT * FROM menuniv WHERE stock=TRUE";
$result = $conn->query($query);
if (!$result) echo "Couldn't get the menu: " . $conn->error;
$num_items = $result->num_rows;
// PROCESS UPDATES from POST
if (isset($_POST['submit'])) {
$ord_feed = "Updated Stock";
for($j = 0; $j < $num_items_total; $j++) {
$query = "UPDATE menuniv SET stock=FALSE WHERE stock = 1 OR 2";
$result = $conn->query($query);
if (!$result) echo "Coundn't default the menu: " . $conn->error;
}
foreach ($_POST['in_stock'] as $item) {
$query = "UPDATE menuniv SET stock=TRUE WHERE pid=$item";
$result = $conn->query($query);
}
} else {
$ord_feed = "";
}
//POPULATE THE MENU BY TYPE and STOCK
$types = get_types();
foreach($types as $type)
{
$query = "SELECT * FROM menuniv WHERE type='$type'";
$menu[$type] = $conn->query($query);
if (!$menu[$type]) echo "Couldn't get the menu for $type: " . $conn->error;
}
// FIND MAX LENGTH OF TYPE COLUMNS
$longest = 0;
foreach($types as $type)
{
$length = $menu[$type]->num_rows;
if($length > $longest) $longest = $length;
}
echo "<html>";
echo file_get_contents("kitchen_head.html");
echo "<body>";
echo file_get_contents("kitchen_header.html");
echo file_get_contents("nav.html");
echo <<<_END
<main>
<h2>What's in stock</h2>
<p>We have <b>$num_items</b> items in stock!</p>
<h4 id='msg'>$ord_feed</h4>
<p>Checked items are stocked. Check or uncheck them accordingly. To add an item to the universal menu, go to <a href='items_props.php'>Update the Menu</a>.
<form action='update_stock.php' method='post'>
<table>
<tr>
_END;
// MAKE COLUMN TITLES
foreach($types as $type) {
echo "<th>$type"."s</th>";
}
echo "</tr>";
// MAKE TABLE ROWS
for($j = 0 ; $j < $longest; ++$j) {
echo "<tr>";
foreach($types as $type) {
$menu[$type]->data_seek($j);
$row = $menu[$type]->fetch_array(MYSQLI_ASSOC);
echo "<td>$row[name]";
if (!empty($row[name]) && isset($_COOKIE['user'])) {
if( $row[stock] ) {
echo "<input type='checkbox'
name='in_stock[]' value='$row[pid]' checked>";
} else {
echo "<input type='checkbox'
name='in_stock[]' value='$row[pid]'>";
}
}
echo "</td>";
}
echo "</tr>";
}
echo "</table>";
// Display submit only when logged in
if (isset($_COOKIE['user'])) {
echo "<input type='submit' name='submit' value='Update Menu'></form>";
} else {
echo "</p><a href='signin.php'>Sign in</a> to make changes to the menu</p>";
}
echo "</main>";
echo file_get_contents("footer.html");
echo "</html>";
$result->close();
$conn->close();
?>