My PHP pagination code is quite messy. I use something like this:
$display = 8;
if(isset($_GET['p'])&&is_numeric($_GET['p'])) {
$pages = $_GET['p'];
} else {
$q = "SELECT COUNT(image_id) FROM images";
$r = mysqli_query($dbc, $q);
$ps = mysqli_fetch_array($r, MYSQL_NUM);
$broj_redova = $ps[0];
if($broj_redova <= 8) {
$pages = 1;
} else {
$pages = ceil($broj_redova / $display);
}
}
if(isset($_GET['s'])&&is_numeric($_GET['s'])) {
$start = $_GET['s'];
} else {
$start = 0;
}
// here is the query
$q = "SELECT * FROM images LIMIT $start, $display";
if($pages > 1) { // make pagination only if there is more than a one page
$current_page = ($start/$display) + 1;
// If it's not the first page, make previous button: *****************************
if($current_page != 1) {
echo '<a href="gallery.php?s='.($start - $display).'&p='.$pages.'">Previous</a>';
}
// ****************************************************************************
for($i = 1; $i <= $pages; $i++) {
if ($i != $current_page) {
echo '<a href=gallery.php?s='.(($display*($i - 1))).'&p='.$pages.'">'.$i.'</a>';
} else {
echo $i . ' ';
}
} // end of foor loop
// if it's not the last page, make next button =***********************************
if($current_page != $pages) {
echo '<a href="gallery.php?s='.($start + $display).'&p='.$pages.'">Next</a>';
}
// *****************************************************************************
}
I wonder if you guys program each pagination code when it's needed or you have something ready made, so when you need you just add it?