Hello. Here I am again:o
Im having problems finding out why my pagination script won't work properly. Im listing out the contents of a mysql table wich I also have dynamic sorting for. This works. Part of the pagination works, but not the part wich should list out all the pages as numeric (1, 2, 3, 4, 5, etc) links below my table.
I would greatly apreciate if someone would take a look at my script and comment on what could be wrong.
Here is my "Frankenstein" script:glare:
<?php echo "<a href=./>Home</a><br>";
// connect to db
$connect = mysqli_connect('localhost','user','pass','db');
// set the default sort
$sort = (isset($_GET['sort'])) ? $_GET['sort'] : 'dato';
// define sort order
$sort_order = 'desc';
if(isset($_GET['sort_by'])) {
if($_GET['sort_by'] == 'asc') {
$sort_order = 'desc';
} else {
$sort_order = 'asc';
}
}
// html table
echo "<table border='1'>";
// query mysql table column keys
$query_keys = mysqli_query($connect,("SELECT * FROM table"));
$fetch_keys = mysqli_fetch_array($query_keys, MYSQLI_ASSOC);
// print out table columns keys
echo "<tr>";
foreach(array_keys($fetch_keys) as $key) {
switch($sort) {
case $key :
$order_by = $key;
break;
}
if($sort==$key) {
echo "<td bgcolor=lightgreen><a href='?sort=$key&sort_by=" . $sort_order . "'>$key</a></td>";
} else {
echo "<td bgcolor=lightblue><a href='?sort=$key&sort_by=" . $sort_order . "'>$key</a></td>";
}
}
echo "</tr>";
// free query keys
mysqli_free_result($query_keys);
$records_per_page = 4;
$total = mysqli_query($connect,("SELECT COUNT(*) FROM table"),0);
$page_count = ceil($total / $records_per_page);
$page = 1;
if (isset($_GET['page']) && $_GET['page'] >= 1 && $_GET['page'] <= $page_count) {
$page = (int)$_GET['page'];
}
$skip = ($page - 1) * $records_per_page;
// query mysql table content
$query_content = mysqli_query($connect,("SELECT * FROM table ORDER BY $sort $sort_order LIMIT $records_per_page"));
// print out table contents
while($row = mysqli_fetch_array($query_content, MYSQLI_ASSOC)) {
echo "<tr>";
foreach($row as $rows) {
echo "<td>" . $rows . "</td>";
}
echo "</tr>";
}
// free query content
mysqli_free_result($query_content);
echo "</table>";
for ($i = 1; $i <= $page_count; ++$i) {
echo "<a href='?page=" . $i . "'>" . $i . "</a> ";
}
// close connection
mysqli_close($connect);
?>