Home > php教程 > php手册 > 学习php分页代码实例

学习php分页代码实例

WBOY
Release: 2016-06-06 20:27:11
Original
1089 people have browsed it

学习PHP,一定会遇到操作MYSQL数据库,而且会对数据库里的数据做分页显示出来的问题,下面写一个小例子学习一下PHP分页的方法。

PHP分页有很多方法,今天我们就用一个小例子演示一下这个功能。

复制代码 代码如下:


$result = "

    ";

        // 上一页
        if ($offset>0) {
            $result .= "

  •             Prev
           
  • ";
        }

        $pages = $allPageNums;  //总页数
        $page = $curPage;    //当前页数
        $page_len = 9;
        $page_len = ($page_len%2)?$page_len:$pagelen+1;//页码个数
        $pageoffset = ($page_len-1)/2;//页码个数左右偏移量
        if($pages>$page_len){
            //如果当前页小于等于左偏移
            if($page            $init=1;
                $max_p = $page_len;
            }else{//如果当前页大于左偏移
                //如果当前页码右偏移超出最大分页数
                if($page+$pageoffset>=$pages+1){
                    $init = $pages-$page_len+1;
             $max_p = $pages;
                }else{
                    //左右偏移都存在时的计算
                    $init = $page-$pageoffset;
                    $max_p = $page+$pageoffset;
                }
            }
        } else {
      
          $init = 1;
          $max_p = $pages;
      }

        for($i=$init; $i
            if ( $i == $curPage ) {
                $result .=    "

  • $i
  • ";
                continue;
            }

            $result .=    "
  • $i
  • ";

        }

        // 打印下一页
        if ( $allnums > ($offset+$maxrow) ) {
            $result .=    "

  •             Next
           
  • ";
        }
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template