-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
categories.php
170 lines (156 loc) · 7.05 KB
/
categories.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
session_start();
require 'db.php';
// Check if user is admin
if (!isset($_SESSION['username']) || $_SESSION['role'] !== 'admin') {
header("Location: index.php");
exit;
}
$success_message = '';
$error_message = '';
// Handle form submission
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// Add Category
if (isset($_POST['action']) && $_POST['action'] == 'add') {
$name = sanitize($_POST['name'], $conn);
$sql = "INSERT INTO categories (name) VALUES ('$name')";
if ($conn->query($sql) === TRUE) {
$success_message = "Category added successfully!";
} else {
$error_message = "Error adding category: " . $conn->error;
}
}
// Edit Category
if (isset($_POST['action']) && $_POST['action'] == 'edit') {
$id = intval($_POST['id']);
$name = sanitize($_POST['name'], $conn);
$sql = "UPDATE categories SET name='$name' WHERE id=$id";
if ($conn->query($sql) === TRUE) {
$success_message = "Category updated successfully!";
} else {
$error_message = "Error updating category: " . $conn->error;
}
}
// Delete Category
if (isset($_POST['action']) && $_POST['action'] == 'delete') {
$id = intval($_POST['id']);
$sql = "DELETE FROM categories WHERE id=$id";
if ($conn->query($sql) === TRUE) {
$success_message = "Category deleted successfully!";
} else {
$error_message = "Error deleting category: " . $conn->error;
}
}
}
// Fetch categories
$categories = $conn->query("SELECT * FROM categories ORDER BY name ASC");
?>
<?php require 'header.php'; require 'nav.php'; ?>
<div class="container mt-4">
<h2>Manage Categories</h2>
<?php if ($success_message): ?>
<div class="alert alert-success alert-dismissible fade show" role="alert">
<?= htmlspecialchars($success_message) ?>
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
<?php endif; ?>
<?php if ($error_message): ?>
<div class="alert alert-danger alert-dismissible fade show" role="alert">
<?= htmlspecialchars($error_message) ?>
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
<?php endif; ?>
<button class="btn btn-primary my-3" data-bs-toggle="modal" data-bs-target="#addCategoryModal">
<i class="fas fa-plus"></i> Add Category
</button>
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>Name</th>
<th style="width:150px;">Actions</th>
</tr>
</thead>
<tbody>
<?php while ($category = $categories->fetch_assoc()): ?>
<tr>
<td><?= htmlspecialchars($category['name']) ?></td>
<td>
<!-- Edit Button -->
<button class="btn btn-sm btn-primary" data-bs-toggle="modal"
data-bs-target="#editCategoryModal<?= $category['id'] ?>">
<i class="fas fa-edit"></i>
</button>
<!-- Delete Button -->
<form method="POST" action="categories.php" style="display:inline-block;"
onsubmit="return confirm('Are you sure you want to delete this category?');">
<input type="hidden" name="action" value="delete">
<input type="hidden" name="id" value="<?= $category['id'] ?>">
<button type="submit" class="btn btn-sm btn-danger">
<i class="fas fa-trash"></i>
</button>
</form>
</td>
</tr>
<!-- Edit Category Modal -->
<div class="modal fade" id="editCategoryModal<?= $category['id'] ?>" tabindex="-1"
aria-labelledby="editCategoryModalLabel<?= $category['id'] ?>" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<form method="POST" action="categories.php">
<div class="modal-header">
<h5 class="modal-title" id="editCategoryModalLabel<?= $category['id'] ?>">Edit Category</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal"
aria-label="Close"></button>
</div>
<div class="modal-body">
<input type="hidden" name="action" value="edit">
<input type="hidden" name="id" value="<?= $category['id'] ?>">
<div class="mb-3">
<label for="name<?= $category['id'] ?>" class="form-label">Category Name</label>
<input type="text" class="form-control" id="name<?= $category['id'] ?>" name="name"
value="<?= htmlspecialchars($category['name']) ?>" required>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary"
data-bs-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Update Category</button>
</div>
</form>
</div>
</div>
</div>
<!-- End Edit Category Modal -->
<?php endwhile; ?>
</tbody>
</table>
</div>
<!-- Add Category Modal -->
<div class="modal fade" id="addCategoryModal" tabindex="-1" aria-labelledby="addCategoryModalLabel"
aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<form method="POST" action="categories.php">
<div class="modal-header">
<h5 class="modal-title" id="addCategoryModalLabel">Add Category</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal"
aria-label="Close"></button>
</div>
<div class="modal-body">
<input type="hidden" name="action" value="add">
<div class="mb-3">
<label for="name" class="form-label">Category Name</label>
<input type="text" class="form-control" id="name" name="name" required>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary"
data-bs-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Save Category</button>
</div>
</form>
</div>
</div>
</div>
<!-- End Add Category Modal -->
<?php require 'footer.php'; ?>