First of all, I didn't write this code from scratch, I'm pretty sure I took it from an online tutorial and modified it slightly to suit my needs. Can the code be improved in any way? I feel it's quite long winded and repetitive for a relatively simple task.
function paginationAdvanced($select,$query,$per_page,$page,$url){
global $con;
$query = mysqli_query($con, "{$select} {$query}") or die(mysql_error());
$total = mysqli_num_rows($query);
$adjacents = "2";
$prevlabel = "Prev";
$nextlabel = "Next";
$lastlabel = "Last";
$page = ($page == 0 ? 1 : $page);
$start = ($page - 1) * $per_page;
$prev = $page - 1;
$next = $page + 1;
$lastpage = ceil($total/$per_page);
$lpm1 = $lastpage - 1; // //last page minus 1
$results_to = $start + $per_page;
if ($results_to > $total) { $results_to = $total; }
$pagination = "";
if($lastpage > 1){
$pagination .= "<nav><ul class='pagination'>";
$pagination .= "<li class='page-item'><a class='page-link active' href='#'>Displaying " . ($start+1) . " to " . $results_to . " of " . $total . " results</a></li>";
//$pagination .= "<li><span class='page_info'>Page {$page} of {$lastpage}</span></li>";
if ($page > 1) $pagination.= "<li><a class='page-link' href='{$url}page={$prev}' aria-label='Previous'><span aria-hidden='true'>«</span><span class='sr-only'>Previous</span></a></li>";
if ($lastpage < 7 + ($adjacents * 2)){
for ($counter = 1; $counter <= $lastpage; $counter++){
if ($counter == $page)
$pagination.= "<li class='page-item active'><a class='page-link'>{$counter}</a></li>";
else
$pagination.= "<li class='page-item'><a class='page-link' href='{$url}page={$counter}'>{$counter}</a></li>";
}
} elseif($lastpage > 5 + ($adjacents * 2)){
if($page < 1 + ($adjacents * 2)) {
for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++){
if ($counter == $page)
$pagination.= "<li class='page-item active'><a class='page-link'>{$counter}</a></li>";
else
$pagination.= "<li class='page-item'><a class='page-link' href='{$url}page={$counter}'>{$counter}</a></li>";
}
$pagination.= "<li class='dot'>...</li>";
$pagination.= "<li class='page-item'><a class='page-link' href='{$url}page={$lpm1}'>{$lpm1}</a></li>";
$pagination.= "<li class='page-item'><a class='page-link' href='{$url}page={$lastpage}'>{$lastpage}</a></li>";
} elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2)) {
$pagination.= "<li class='page-item'><a class='page-link' href='{$url}page=1'>1</a></li>";
$pagination.= "<li class='page-item'><a class='page-link' href='{$url}page=2'>2</a></li>";
$pagination.= "<li class='dot page-item'>...</li>";
for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++) {
if ($counter == $page)
$pagination.= "<li class='page-item active'><a class='page-link'>{$counter}</a></li>";
else
$pagination.= "<li class='page-item'><a class='page-link' href='{$url}page={$counter}'>{$counter}</a></li>";
}
$pagination.= "<li class='dot page-item'>..</li>";
$pagination.= "<li class='page-item'><a class='page-link' href='{$url}page={$lpm1}'>{$lpm1}</a></li>";
$pagination.= "<li class='page-item'><a class='page-link' href='{$url}page={$lastpage}'>{$lastpage}</a></li>";
} else {
$pagination.= "<li class='page-item'><a class='page-link' href='{$url}page=1'>1</a></li>";
$pagination.= "<li class='page-item'><a class='page-link' href='{$url}page=2'>2</a></li>";
$pagination.= "<li class='dot'>..</li>";
for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++) {
if ($counter == $page)
$pagination.= "<li class='page-item active'><a class='page-link'>{$counter}</a></li>";
else
$pagination.= "<li class='page-item'><a class='page-link' href='{$url}page={$counter}'>{$counter}</a></li>";
}
}
}
if ($page < $counter - 1) {
$pagination.= "<li class='page-item'><a class='page-link' aria-label='Next' href='{$url}page={$next}'><span aria-hidden='true'>»</span><span class='sr-only'>Next</span></a></li>";
$pagination.= "<li class='page-item'><a class='page-link' aria-label='Last' href='{$url}page=$lastpage'><span aria-hidden='true'>{$lastlabel}</span><span class='sr-only'>Last</span></a></li>";
}
$pagination.= "</ul></nav>";
}
return $pagination;
}
paginationAdvanced()
that will be converted to html somewhere else. This will make your code shorter and more readable, which in turn might inspire other users check and improve your code. \$\endgroup\$$lastpage < 7 + ($adjacents * 2)
. If those are actually magic numbers (e.g. you decided at some point you wanted to have 7 pages displayed), consider replacing them with a variable. \$\endgroup\$