Modify a pagination script to show at most 9 page links

Here is some php pagination code, it can run smoothly.

$rst =  mysql_query($qry) or die(mysql_error());
        $numrows    =   mysql_num_rows($rst);
        $qry         .= " limit $starting, $recpage";
        $this->result   =   mysql_query($qry) or die(mysql_error());
        $next       =   $starting+$recpage;
        $var        =   ((intval($numrows/$recpage))-1)*$recpage;
        $page_showing   =   intval($starting/$recpage)+1;
        $total_page =   ceil($numrows/$recpage); 



    $norepeat = 4;
    $j = 1;
    for($i=$page_showing; $i>1; $i--){
        $fpreviousPage = $i-1;
        $pagee = ceil($fpreviousPage*$recpage)-$recpage;
        $anch = "<a href='#' >$fpreviousPage</a>".$anch;
        if($j == $norepeat) break;
        $j++;
    }
    $anc .= $anch;  // last 4

    $anc .= $page_showing;   //current page

    $j = 1;
    for($i=$page_showing; $i<$total_page; $i++){
        $fnextPage = $i+1;
        $pagee = ceil($fnextPage*$recpage)-$recpage;
        $anc .= "<a href='#' >$fnextPage</a>"; // next 4
        if($j==$norepeat) break;
        $j++;
    }

but it only display prev and next 4 pages max. like

(1) | 2 | 3 | 4 | 5   // (1) as current page
2 | 3 | 4 | 5 | (6) | 7 | 8 | 9 | 10  // (6) as current page
12 | 13 | 14 | 15 | (16) // (16) as current page, the last page

now, how to modify the code, so that make a display like:

(1) | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9  // (1) as current page
2 | 3 | 4 | 5 | (6) | 7 | 8 | 9 | 10  // (6) as current page
8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | (16) // (16) as current page, the last page

there will show at least 9 pages pagination.

still need avoid if the total pages less than 9,

(1) | 2 | 3 | 4 | 5 | 6 | 7 // (1) as current page, 7 pages all, show max 7, not 9

You’ll have to decide how many previous and next pages you need to display (instead of using 4 as a fixed value). Try this (didn’t test it):


// decide max previous and next values
$maxprevious = $page_showing - 1;
$maxnext = $totalpage - $page_showing;

// decide how many next and previous to show based on default value
$norepeat = 4;
if ($maxprevious < $norepeat) {
  $noprevious = $maxprevious;
  $nonext = ($norepeat * 2) - $noprevious;
} else {
  if ($maxnext < $norepeat) {
    $nonext = $maxnext;
    $noprevious = ($norepeat * 2) - $nonext;
  } else {
    $noprevious = $norepeat;
    $nonext = $norepeat;
  }
}

$j = 1;     
for ($i=$page_showing; $i>1; $i--) {       
  $fpreviousPage = $i-1;       
  $pagee = ceil($fpreviousPage*$recpage)-$recpage;      
  $anch = "<a href='#' >$fpreviousPage</a>".$anch;       
  if ($j == $noprevious) break;       
  $j++;     
}     

$anc .= $anch;  // last n      

$anc .= $page_showing;   //current page      

$j = 1;     
for ($i = $page_showing; $i < $total_page; $i++) {       
  $fnextPage = $i+1;       
  $pagee = ceil($fnextPage*$recpage)-$recpage;       
  $anc .= "<a href='#' >$fnextPage</a>"; // next n       
  if ($j == $nonext) break;       
  $j++;     
}  

@guido2004, great help. it works fine. thank u very much :slight_smile: