Modify a pagination script to show at most 9 page links

Discussion in 'PHP' started by cj333, May 24, 2011.

  1. #1
    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++; 
        }  
    PHP:
    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
    Code (markup):
    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. 
    Code (markup):
    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
    Code (markup):

     
    cj333, May 24, 2011 IP