-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcategory.php
300 lines (237 loc) · 9.08 KB
/
category.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
<?php
include_once('config/connectdb.php');
session_start();
if($_SESSION['useremail']=="" OR $_SESSION['role']=="User"){
header('location:index.php');
}
include_once('inc/header.php');
// Insert Data into database (Save Button)
if(isset($_POST['btnsave'])) {
$category = $_POST['txtcategory'];
if(empty($category)) {
$error = '<script type="text/javascript">
jQuery(function validation(){
swal({
title:"Error",
text: "Category name is required.",
icon: "error",
button: "Try Again",
});
});
</script>';
echo $error;
}
//if(!isset($error)) {
else {
$insert = $pdo->prepare("INSERT INTO tbl_category(category) VALUES (:category)");
$insert->bindParam(':category', $category);
$result = $insert->execute();
if ($result) {
echo '<script type="text/javascript">
jQuery(function validation(){
swal({
title:"Success",
text: "New Category Added",
icon: "success",
button: "Close",
});
});
</script>';
} else {
echo '<script type="text/javascript">
jQuery(function validation(){
swal({
title:"Error",
text: "Check Again",
icon: "error",
button: "Try Again",
});
});
</script>';
}
}
} // Code for Save Button Ends Here
// Start of Update Button to INSERT data into Database.
if (isset($_POST['btnupdate'])) {
$category = $_POST['txtcategory'];
$id = $_POST['txtid'];
if(empty($category)) {
$error_update = '<script type="text/javascript">
jQuery(function validation(){
swal({
title:"Error",
text: "Category name is required.",
icon: "error",
button: "Try Again",
});
});
</script>';
echo $error_update;
}
if(!isset($error_update)) {
$update = $pdo->prepare("UPDATE tbl_category SET category= :category WHERE catid =" .$id);
$update->bindParam(':category', $category);
//$update->bindParam(':id', $id);
if($update->execute()) {
echo '<script type="text/javascript">
jQuery(function validation(){
swal({
title:"Updated",
text: "Category name is Updated.",
icon: "success",
button: "Close",
});
});
</script>';
} else {
echo '<script type="text/javascript">
jQuery(function validation(){
swal({
title:"Error",
text: "Someting went wrong.",
icon: "error",
button: "Try Again",
});
});
</script>';
}
}
} // btnupdate ends here.
// Start code for Delete Button
if (isset($_POST['btndelete'])) {
$delete = $pdo->prepare("DELETE FROM tbl_category WHERE catid=".$_POST['btndelete']);
$result = $delete->execute();
if ($result) {
echo '<script type="text/javascript">
jQuery(function validation(){
swal({
title:"Deleted",
text: "Record Deleted.",
icon: "success",
button: "Close",
});
});
</script>';
} else {
echo '<script type="text/javascript">
jQuery(function validation(){
swal({
title:"Error",
text: "Someting went wrong.",
icon: "error",
button: "Try Again",
});
});
</script>';
}
} // end of delete button code
?>
<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper">
<!-- Content Header (Page header) -->
<section class="content-header">
<h1>
Categories.
<small>Add / Remove / Edit Categories.</small>
</h1>
<ol class="breadcrumb">
<li><a href="#"><i class="fa fa-dashboard"></i> Level</a></li>
<li class="active">Here</li>
</ol>
</section>
<!-- Main content -->
<section class="content container-fluid">
<!--------------------------
| Your Page Content Here |
-------------------------->
<div class="box box-primary">
<div class="box-header with-border">
<h3 class="box-title">All fields are required. </h3>
</div>
<!-- /.box-header -->
<!-- form start -->
<div class="box-body">
<form action="" method="POST">
<?php
// Code for for Edit Button
if(isset($_POST['btnedit'])) {
$select = $pdo->prepare(" SELECT *
FROM tbl_category
WHERE catid=".$_POST['btnedit']);
$select->execute();
if($select) {
$record = $select->fetch(PDO::FETCH_OBJ);
echo '
<div class="col-md-4">
<div class="form-group">
<label>Category:</label>
<input type="hidden" class="form-control" value="'.$record->catid.'"name="txtid">
<input type="text" class="form-control" value="'.$record->category.'" name="txtcategory" placeholder="Enter Category Name">
</div>
<div class="box-footer">
<button type="submit" name="btnupdate" class="btn btn-primary">Update Category</button>
</div>
';
}
} else {
echo '
<div class="col-md-4">
<div class="form-group">
<label>Category:</label>
<input type="text" class="form-control" name="txtcategory" placeholder="Enter Category Name">
</div>
<div class="box-footer">
<button type="submit" name="btnsave" class="btn btn-primary">Add Category</button>
</div>
';
}
?>
</div>
<div class="col-md-8">
<h3>Existing Categories</h3>
<table id="table_category" class="table table-stripped">
<thead>
<tr>
<th>No.</th>
<th>Category</th>
<th class="text-center">Edit Record</th>
<th class="text-center">Delete Record</th>
</tr>
</thead>
<tbody>
<?php
$select = $pdo->prepare("SELECT * FROM tbl_category");
$select->execute();
while($row = $select->fetch(PDO::FETCH_OBJ)) {
echo'<tr>
<td>'.$row->catid.'</td>
<td>'.$row->category.'</td>
<td>
<button type = "submit" class="btn btn-success btn-sm" value = "'.$row->catid.'" name="btnedit" >Edit</button>
</td>
<td>
<button type="submit" class = "btn btn-danger btn-sm" value = "'.$row->catid.'" name="btndelete">Delete</button>
</td>
</tr>';
}
?>
</tbody>
</table>
</div>
</form>
</div>
<!-- /.box-body -->
</div>
</section>
<!-- /.content -->
</div>
<!-- /.content-wrapper -->
<!-- Call Single Function for DataTable -->
<script>
$(document).ready(function () {
$('#table_category').DataTable()
});
</script>
<?php
include_once('inc/footer.php');
?>