Skip to content
This repository was archived by the owner on May 18, 2021. It is now read-only.

Commit f25870f

Browse files
author
cammygames
committed
Multi Page Support For Staff List
1 parent 45d8e02 commit f25870f

File tree

1 file changed

+64
-3
lines changed

1 file changed

+64
-3
lines changed

views/admin.php

Lines changed: 64 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// create a database connection, using the constants from config/db.php (which we loaded in index.php)
33
$db_connection = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
44

5+
$page_rows = results_per_page;
6+
57
// change character set to utf8 and check it
68
if (!$db_connection->set_charset("utf8")) {
79
$db_connection->errors[] = $db_connection->error;
@@ -93,15 +95,41 @@
9395
<?php
9496
if (!$db_connection->connect_errno)
9597
{
98+
if (!(isset($_POST['pagenum'])))
99+
{
100+
$pagenum = 1;
101+
}
102+
else
103+
{
104+
$pagenum = $_POST['pagenum'];
105+
}
106+
107+
$sql = "SELECT * FROM `users`;";
108+
109+
$result_of_query = $db_connection->query($sql);
110+
$rows = mysqli_num_rows($result_of_query);
96111

97-
if (!empty($_POST))
112+
$last = ceil($rows/$page_rows);
113+
114+
if ($pagenum < 1)
115+
{
116+
$pagenum = 1;
117+
}
118+
elseif ($pagenum > $last)
119+
{
120+
$pagenum = $last;
121+
}
122+
123+
$max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows;
124+
125+
if (isset($_POST['searchText']))
98126
{
99127
$searchText = $_POST['searchText'];
100-
$sql = "SELECT * FROM `users` WHERE `user_name` LIKE '%".$searchText."%' ;";
128+
$sql = "SELECT * FROM `users` WHERE `user_name` LIKE '%".$searchText."%' ".$max." ;";
101129
}
102130
else
103131
{
104-
$sql = "SELECT * FROM `users`;";
132+
$sql = "SELECT * FROM `users` ".$max.";";
105133
}
106134

107135
$result_of_query = $db_connection->query($sql);
@@ -119,6 +147,39 @@
119147
echo "</form></td>";
120148
echo "</tr>";
121149
};
150+
echo "</tbody></table>";
151+
echo "<table><thead>";
152+
echo "<br>";
153+
if ($pagenum == 1){}
154+
else
155+
{
156+
echo "<th><form method='post' action='".$_SERVER['PHP_SELF']."' name='pagenum'>";
157+
echo "<input id='pagenum' type='hidden' name='pagenum' value='1'>";
158+
echo "<input type='submit' value=' <<-First '>";
159+
echo "</form></th>";
160+
$previous = $pagenum-1;
161+
echo "<th><form style='float:right;' method='post' action='".$_SERVER['PHP_SELF']."' name='pagenum'>";
162+
echo "<input id='pagenum' type='hidden' name='pagenum' value='".$previous."'>";
163+
echo "<input type='submit' value=' <-Previous '>";
164+
echo "</form></th>";
165+
}
166+
//This does the same as above, only checking if we are on the last page, and then generating the Next and Last links
167+
if ($pagenum == $last) {}
168+
else
169+
{
170+
$next = $pagenum+1;
171+
echo "<th><form method='post' action='".$_SERVER['PHP_SELF']."' name='pagenum'>";
172+
echo "<input id='pagenum' type='hidden' name='pagenum' value='".$next."'>";
173+
echo "<input type='submit' value=' Next -> '>";
174+
echo "</form></th>";
175+
echo " ";
176+
echo "<th><form method='post' action='".$_SERVER['PHP_SELF']."' name='pagenum'>";
177+
echo "<input id='pagenum' type='hidden' name='pagenum' value='".$last."'>";
178+
echo "<input type='submit' value=' Last ->> '>";
179+
echo "</form></th>";
180+
}
181+
echo "</thead></table>";
182+
122183
}
123184
else
124185
{

0 commit comments

Comments
 (0)