修改分页脚本以最多显示9个页面链接

问题描述

| 这是一些PHP分页代码,它可以平稳运行。
        $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++;
    }
但最多只能显示上一页和后4页。喜欢
(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
现在,如何修改代码,以便显示如下:
(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
至少会显示9页的分页。 如果总页数少于9,仍然需要避免
(1) | 2 | 3 | 4 | 5 | 6 | 7 // (1) as current page,7 pages all,show max 7,not 9.
谢谢。     

解决方法

$norepeat = 9;
然后
 $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($totalpage>=9){ if($j==$norepeat) break;}else { if($j==$totalpage) break;}
        $j++;
    }