-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathitems.php
143 lines (135 loc) · 6.06 KB
/
items.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
include "Functions.php";
$navs = get_nav(3);
include "header.php";
$category = new Category($db);
$item = new Item($db);
$categories = $category->get_categories();
if(count($_POST)) {
$id = !empty($_POST["id"]) ? $_POST["id"] : null;
$name = !empty($_POST["name"]) ? $_POST["name"] : null;
$price = !empty($_POST["price"]) ? $_POST["price"] : null;
$categoryID = !empty($_POST["categoryID"]) ? $_POST["categoryID"] : null;
$quantity = !empty($_POST["quantity"]) ? $_POST["quantity"] : null;
$action= !empty($_POST["action"]) ? $_POST["action"] : null;
$_SESSION["error"] = null;
switch ($action) {
case "insert":
$item->add_item([
"name" => $name,
"price" => $price,
"category" => $categoryID,
"quantity" => $quantity,
]);
break;
case "update":
$item->change_item([
"id" => $id,
"name" => $name,
"price" => $price,
"category" => $categoryID,
"quantity" => $quantity,
]);
break;
case "delete":
$item->delete_item($id);
break;
}
header("Location:items.php");
}
if (!isset($_GET["id"])) {
$items = $item->get_items();
$items = $items ? $items : [];
?>
<article class="container">
<div class="row">
<h3 class="col-md-offset-1 col-md-4">Նոր ապրանք</h3>
<h5><?= !empty($_SESSION['error']) ? $_SESSION["error"] : ""; ?></h5>
</div>
<div class="row">
<form class="col-md-offset-1 col-md-10" method="post" action="items.php">
<div class="form-group">
<label for="name">Անուն</label>
<input type="text" id="name" class="form-control" name="name" placeholder="Անուն" required>
</div>
<div class="form-group">
<label for="պռիցե">Գին</label>
<input type="text" id="պռիցե" class="form-control" name="price" placeholder="Գին" required>
</div>
<div class="form-group">
<label for="categoryID">Կատեգորիա</label>
<select id="categoryID" class="form-control" name="categoryID" required>
<option selected disabled value="">Ընտրեք կատեգորիան</option>
<?php foreach($categories as $category) { ?>
<option value="<?= $category["ID"]; ?>"><?= $category["Name"]; ?></option>
<?php } ?>
</select>
</div>
<div class="form-group">
<label for="quantity">Քանակ</label>
<input type="number" id="quantity" class="form-control" name="quantity" placeholder="Քանակ" required>
</div>
<input type="hidden" name="action" value="insert">
<button type="submit" class="btn btn-primary">Ավելացնել</button>
</form>
</div>
<div class="row">
<div class="list-group container-fluid">
<?php foreach($items as $item) { ?>
<a href="items.php?id=<?= $item["ID"]; ?>" class="list-group-item row">
<div class="col-xs-1 text-center"><?= $item["ID"]; ?></div>
<div class="col-xs-4"><?= $item["Name"]; ?></div>
</a>
<?php } ?>
</div>
</div>
</article>
<?php
} else {
$id = $_GET["id"];
$it = $item->get_item($id);
?>
<article class="container">
<div class="row">
<form class="col-md-offset-1 col-md-10" method="post" action="items.php">
<div class="form-group">
<h3 class="bold">Կոդ <?= $id; ?></h3>
</div>
<div class="form-group">
<label for="name">Անուն</label>
<input type="text" id="name" class="form-control" name="name" placeholder="Անուն" required value="<?= $it["Name"]; ?>">
</div>
<div class="form-group">
<label for="price">Գին</label>
<input type="text" id="price" class="form-control" name="price" placeholder="Գին" required value="<?= $it["Price"]; ?>">
</div>
<div class="form-group">
<label for="categoryID">Կատեգորիա</label>
<select id="categoryID" class="form-control" name="categoryID" required>
<option selected disabled value="">Ընտրեք կատեգորիան</option>
<?php foreach($categories as $category) { ?>
<option <?= $category["ID"] == $it["CategoryID"] ? "selected" : ""; ?> value="<?= $category["ID"]; ?>"><?= $category["Name"]; ?></option>
<?php } ?>
</select>
</div>
<div class="form-group">
<label for="quantity">Քանակ</label>
<input type="number" id="quantity" class="form-control" name="quantity" placeholder="Քանակ" required value="<?= $it["Quantity"]; ?>">
</div>
<input type="hidden" name="id" placeholder="Անուն" value="<?= $it["ID"]; ?>">
<input type="hidden" name="action" value="update">
<button type="submit" class="btn btn-primary">Փոխել</button>
</form>
</div>
<div class="row">
<form class="col-md-offset-1 col-md-10" method="post" action="items.php">
<input type="hidden" name="id" placeholder="Անուն" value="<?= $it["ID"]; ?>">
<input type="hidden" name="action" value="delete">
<button type="submit" class="btn btn-danger">Հեռացնել</button>
</form>
</div>
</article>
<?php
}
include "footer.php";
?>