I’m having a problem with my PHP pagination for a project.
It almost works but it doesn’t seem to display the numbers correctly.
I want only 6 more page numbers to display after the selected and one before;
(also if you are on page one display 7 after)
For example:
If on Page 1: 1/2/3/4/5/6/7/8
If on Page 2: 1/2/3/4/5/6/7/8
If on Page 5: 4/5/6/7/8/9/10/11
If on Page 10: 9/10/11/12/13/14/15/16
This is my code so far…
if($page == ceil($NumOfPages) && $page != 1){
for($i = 1; $i <= ceil($NumOfPages)-1; $i++){
if($i > 0){
echo "<a href=\"/{$i}\">{$i}</a>";
}
}
}
if ($page == ceil($NumOfPages) ) {
$startPage = $page;
}else{
$startPage = 1;
}
for ($i = $startPage; $i <= $page+6; $i++){
if ($i <= ceil($NumOfPages)){
if($i == $page) {
echo "<a href='/page/$i/' title='View movies page $i' id='pagelisel'>$i</a> ";
}else{
echo "<a href='/page/$i/' title='View movies page $i' id='pageli'>$i</a> ";
}
}
}
Any help would be greatly appreciated,
Thanks!
I assumed that (partly for myself… 😉 ):
$pageis the selected page$startPageis the first page number you want to show$numPagesis alreadyceil-edFirst you need to find
$startPage. Depending whether$pageis the first one (ie has the value one 1, another assumption) or not. Your check is slightly off, as it check if it is equal to the last page.Then you need to find out the last page number you want to print (
$lastPage). So check if$startPageis near the end and set ~$lastPage` accordingly:Finally, use you
for-loop which seem ok, but loop from$startPageto$endPage.