-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
97 lines (80 loc) · 3.12 KB
/
index.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
<?php
include_once "style.php";
?>
<body>
<div class="col-md-12 col-sm-12 col-xs-12">
<div class="x_panel">
<h3 id="glyphicons" class="page-header">Product Management
<small>Simple CRUD by KhoaHA</small>
</h3>
<div class="x_content">
<a class="btn btn-primary" data-target=".bs-example-modal-sm" href="create.php">
<span class="glyphicon glyphicon-plus" aria-hidden="true"></span> Create
</a>
<table class="table table-striped">
<thead>
<tr>
<th>ID</th>
<th>Product Name</th>
<th>Description</th>
<th>Price</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
// include database connection
include 'database.php';
$action = isset($_GET['action']) ? $_GET['action'] : "";
// if it was redirected from delete.php
if ($action == 'deleted') {
echo "<div>Record was deleted.</div>";
}
// select all data
$query = "SELECT id, name, description, price FROM products";
$stmt = $con->prepare($query);
$stmt->execute();
// this is how to get number of rows returned
$num = $stmt->rowCount();
//check if more than 0 record found
if ($num > 0) {
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
extract($row);
// creating new table row per record
echo "<tr>";
echo "<td>{$id}</td>";
echo "<td>{$name}</td>";
echo "<td>{$description}</td>";
echo "<td>${$price}</td>";
echo "<td>";
// we will use this links on next part of this post
echo "<a class='collapse-link' href='update.php?id={$id}' ><i class='fa fa-wrench'></i></a>";
echo " / ";
// we will use this links on next part of this post
echo "<a href='#' onclick='delete_user({$id});' class='close-link'><i class='fa fa-close'></i></a>";
echo "</td>";
echo "</tr>";
}
// end table
echo "</table>";
} // if no records found
else {
echo "<div>No records found.</div>";
}
?>
</tbody>
</table>
</div>
</div>
</div>
<script type='text/javascript'>
function delete_user(id) {
var answer = confirm('Are you sure?');
if (answer) {
// if user clicked ok,
// pass the id to delete.php and execute the delete query
window.location = 'delete.php?id=' + id;
}
}
</script>
</body>