|
2 | 2 | // create a database connection, using the constants from config/db.php (which we loaded in index.php) |
3 | 3 | $db_connection = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME); |
4 | 4 |
|
| 5 | + $page_rows = results_per_page; |
| 6 | + |
5 | 7 | // change character set to utf8 and check it |
6 | 8 | if (!$db_connection->set_charset("utf8")) { |
7 | 9 | $db_connection->errors[] = $db_connection->error; |
|
93 | 95 | <?php |
94 | 96 | if (!$db_connection->connect_errno) |
95 | 97 | { |
| 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); |
96 | 111 |
|
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'])) |
98 | 126 | { |
99 | 127 | $searchText = $_POST['searchText']; |
100 | | - $sql = "SELECT * FROM `users` WHERE `user_name` LIKE '%".$searchText."%' ;"; |
| 128 | + $sql = "SELECT * FROM `users` WHERE `user_name` LIKE '%".$searchText."%' ".$max." ;"; |
101 | 129 | } |
102 | 130 | else |
103 | 131 | { |
104 | | - $sql = "SELECT * FROM `users`;"; |
| 132 | + $sql = "SELECT * FROM `users` ".$max.";"; |
105 | 133 | } |
106 | 134 |
|
107 | 135 | $result_of_query = $db_connection->query($sql); |
|
119 | 147 | echo "</form></td>"; |
120 | 148 | echo "</tr>"; |
121 | 149 | }; |
| 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 | + |
122 | 183 | } |
123 | 184 | else |
124 | 185 | { |
|
0 commit comments