-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
169 lines (141 loc) · 3.89 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
158
159
160
161
162
163
164
165
166
167
168
169
<html>
<head>
<title>Employee Database Viewer</title>
<style>
a {
padding: 0px 10px;
word-wrap: normal;
display: inline-block;
}
</style>
<?php include "db.php" ?>
</head>
<body>
<h1>
Welcome to the MySQL Employee Sample Database Viewer
</h1>
<form
method="get"
action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<!-- Provides 4 possible number of entries per page-->
<h3>Select page size:</h3>
<select name="entries">
<option value="15">15</option>
<option value="25">25</option>
<option value="50">50</option>
<option value="75">75</option>
</select>
<h3>Choose sort order:</h3>
<select name="order">
<option value="L_ASC">Last Name (ascending)</option>
<option value="L_DESC">Last Name (descending)</option>
<option value="F_ASC">First Name (ascending)</option>
<option value="F_DESC">First Name (descending)</option>
<option value="E_ASC">Employee Number (ascending)</option>
<option value="E_DESC">Employee Number (descending)</option>
</select>
<input type="submit">
</form>
<?php
$neworder = '';
$order = "ASC";
$type ="first_name";
$slots = array();
if (!empty($_GET["order"])) {
$order = $_GET["order"];
$slots = explode("_", $order);
$neworder = $slots[1];
switch ($slots[0]) {
case "E":
$type = "emp_no";
break;
case "F":
$type = "first_name";
break;
case "L":
default:
$type = "last_name";
break;
}
}
$self = $_SERVER['PHP_SELF'];
if($neworder != '') {
if(!empty($_GET['entries'])) {
$rec_limit = $_GET['entries'];
//sets entries per page
$link = mysqli_connect($host, $user, $pass);
$db = mysqli_select_db($link, $dbname);
// establishes link to sql
$data = mysqli_query($link,"SELECT
COUNT(emp_no)
AS
'total'
FROM
employees");
$rowss = mysqli_fetch_assoc($data);
$row_count = $rowss['total'];
//get current page number and set page/offset accordingly
if(isset($_GET{'page'} ) ) {
$page = $_GET{'page'};
$offset = $rec_limit * $page ;
}
else {
$page = 0;
$offset = 0;
}
//calculate amount of entries left
$left_rec = $row_count - ($page * $rec_limit);
// creates query and result
$sql = "SELECT *
FROM
employees
ORDER BY $type $neworder
LIMIT
$offset, $rec_limit";
$retval = mysqli_query( $link, $sql );
echo '<table border="1">';
echo '<tr>';
// create table header
echo"<th>First Name</th>";
echo"<th>Last Name</th>";
echo"<th>Gender</th>";
echo"<th>Employee Number</th>";
echo"<th>Birth Date</th>";
echo"<th>Hire Date</th>";
echo"<th colspan='3'>Options</th>";
while ($row = mysqli_fetch_assoc($retval)) {
echo"<tr>";
$f = $row['first_name'];
echo"<td>".$f."</td>";
$l= $row['last_name'];
echo"<td>".$l."</td>";
$g= $row['gender'];
echo"<td>".$g."</td>";
$e = $row['emp_no'];
echo"<td>".$e."</td>";
$b= $row['birth_date'];
echo"<td>".$b."</td>";
$h= $row['hire_date'];
echo"<td>".$h."</td>";
# echo "<td><a href = \"delete.php?ID=$e\">Delete</a></td>";
# echo "<td><a href = \"update.php?ID=$e\">Update</a></td>";
echo "<td><a href = \"view.php?ID=$e\">View</a></td>";
echo"</tr>";
}
} echo "</table>";
echo "<br>";
// display paging data
if( $page > 0 ) {
$last = $page - 1;
$next = $page + 1;
echo "<a href = \"$self?page=$last&entries=$rec_limit&order=$order\">Last $rec_limit Records</a> |";
echo "<a href = \"$self?page=$next&entries=$rec_limit&order=$order\">Next $rec_limit Records</a>";
}
else if( $page == 0 ) {
$page = $page + 1;
echo "<a href = \"$self?page=$page&entries=$rec_limit&order=$order\">Next $rec_limit Records</a>";
}
}
?>
</body>
</html>