-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
157 lines (125 loc) · 4.67 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
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
<!DOCTYPE HTML>
<html>
<head>
<title>PDO - Read Records - PHP CRUD Tutorial</title>
<!-- Latest compiled and minified Bootstrap CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />
<!-- custom css -->
<style>
.m-r-1em{ margin-right:1em; }
.m-b-1em{ margin-bottom:1em; }
.m-l-1em{ margin-left:1em; }
.mt0{ margin-top:0; }
</style>
</head>
<body>
<!-- container -->
<div class="container">
<div class="page-header">
<h1>Read Products</h1>
</div>
<!-- PHP code to read records will be here -->
<?php
include 'config/database.php';
// include database connection
// PAGINATION VARIABLES
// page is the current page, if there's nothing set, default is page 1
// $page = isset($_GET['page']) ? $_GET['page'] : 1;
// set records or rows of data per page
//$records_per_page = 5;
// calculate for the query LIMIT clause
// $from_record_num = ($records_per_page * $page) - $records_per_page;
// delete message prompt will be here
$action = isset($_GET['action']) ? $_GET['action'] : "";
// if it was redirected from delete.php
if($action=='deleted'){
echo "<div class='alert alert-success'>Record was deleted.</div>";
}
// select all data
// select data for current page
$query = "SELECT id, name, description, price FROM products ORDER BY id DESC
";
$stmt = $con->prepare($query);
// $stmt->bindParam(":from_record_num", $from_record_num, PDO::PARAM_INT);
// $stmt->bindParam(":records_per_page", $records_per_page, PDO::PARAM_INT);
$stmt->execute();
// this is how to get number of rows returned
$num = $stmt->rowCount();
// link to create record form
echo "<a href='create.php' class='btn btn-primary m-b-1em'>Create New Product</a>";
//check if more than 0 record found
if($num>0){
// data from database will be here
echo "<table class='table table-hover table-responsive table-bordered'>";//start table
//creating our table heading
echo "<tr>";
echo "<th>ID</th>";
echo "<th>Name</th>";
echo "<th>Description</th>";
echo "<th>Price</th>";
echo "<th>Action</th>";
echo "</tr>";
// table body will be here
// retrieve our table contents
// fetch() is faster than fetchAll()
// http://stackoverflow.com/questions/2770630/pdofetchall-vs-pdofetch-in-a-loop
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
// extract row
// this will make $row['firstname'] to
// just $firstname only
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>";
// read one record
echo "<a href='read_one.php?id={$id}' class='btn btn-info m-r-1em'>Read</a>";
// we will use this links on next part of this post
echo "<a href='update.php?id={$id}' class='btn btn-primary m-r-1em'>Edit</a>";
// we will use this links on next part of this post
echo "<a href='#' onclick='delete_user({$id});' class='btn btn-danger'>Delete</a>";
echo "</td>";
echo "</tr>";
}
// end table
echo "</table>";
// PAGINATION
// count total number of rows
// $query = "SELECT COUNT(*) as total_rows FROM products";
//$stmt = $con->prepare($query);
// execute query
//$stmt->execute();
// get total rows
//$row = $stmt->fetch(PDO::FETCH_ASSOC);
//$total_rows = $row['total_rows'];
// paginate records
//$page_url="index.php?";
// include_once "paging.php";
}
// if no records found
else{
echo "<div class='alert alert-danger'>No records found.</div>";
}
?>
</div> <!-- end .container -->
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<!-- Latest compiled and minified Bootstrap JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<!-- confirm delete record will be here -->
<script type='text/javascript'>
// confirm record deletion
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>
</html>