Home >Web Front-end >H5 Tutorial >Page pagination subscript generation code (PHP backend method)

Page pagination subscript generation code (PHP backend method)

PHP中文网
PHP中文网Original
2017-06-07 14:32:191794browse

Test legend:

Page pagination subscript generation code (PHP backend method)

Rendering:

Page pagination subscript generation code (PHP backend method)

Implementation code:

 /**
   *
   * @param $page   页码(1至正无穷)
   * @param $num   数据中多上行为一页
   * @param $rows   数据的总行数
   * @param $length  下标的最大长度
   * @return array
   */
  public function PageDate($page, $num , $rows , $length){
    //初始化数据
    $MaxPage = 0;         //最大页码
    $MinPage = 0;         //最小页码
    $ServerPage = 0;        //下拉框中显示的页码
    $PageData = array();      //全部的页码集合(用户下拉框选择)
    $PageShowData = array();    //用于显示下标的页码集合
    $page = intval($page);
 
    if($rows == 0){
      return $data = array(
        'page' => $page,
        'MaxPage' => $MaxPage,
        'MinPage' => $MinPage,
        'ServerPage' => $ServerPage,
        'PageData' => $PageData,
        'PageShowData' => $PageShowData
      );
    }
 
    //得到最大分页和最小分页
    $MaxPage = intval($rows / $num);
    $double = $rows % $num;
    if($double > 0.000000001)
    {
      $MaxPage += 1;
    }
    $MinPage = 1;
    $ServerPage = 1;
    //得到页码的全部项目
    for($i = 1; $i <= $MaxPage; $i++ ){
      $PageData[$i] = $i;
    }
    //对页码进行正确性改正
    if($page < $MinPage){
      $page = 1;
    }
 
    if($page > $MaxPage){
      $page = 1;
    }
    /*生成显示的页码标签*/
      //生成左侧的代码快
      if($page == $MinPage){
        $PageShowData[1] = $page;
      }
      if($page > $MinPage){
        $PageShowData[1] = $MinPage;
//        $PageShowData[2] = $page - 1; // <<
        $PageShowData[2] = &#39;<<&#39;; // <<
        if($page -2 > $MinPage ){
          $PageShowData[3] = $page - 2;
          $PageShowData[4] = $page - 1;
        }elseif($page -2 == $MinPage){
          $PageShowData[3] = $page - 1;
        }
        //确定选中的页码代码快
        $PageShowData[count($PageShowData) + 1] = $page;
      }
    $COUNT = count($PageShowData) + 1;
    //生成右侧的代码块
      if($page == $MaxPage){
        return $data = array(
          &#39;page&#39; => $page,
          &#39;MaxPage&#39; => $MaxPage,
          &#39;MinPage&#39; => $MinPage,
          &#39;ServerPage&#39; => $ServerPage,
          &#39;PageData&#39; => $PageData,
          &#39;PageShowData&#39; => $PageShowData
        );
      }
      if($page < $MaxPage) {
        if($MaxPage - $page >= 2){
          $j = 1;
          for($i = $COUNT; $i >$PageShowData[$COUNT] = &#39;>>&#39;;
        $PageShowData[$COUNT + 1] = $MaxPage;
 
        //得到选中的参数
        $ServerPage = $PageShowData[$COUNT-1] + 1;
        if($ServerPage > $MaxPage){
          $ServerPage = 1;
        }
 
        return $data = array(
          &#39;page&#39; => $page,
          &#39;MaxPage&#39; => $MaxPage,
          &#39;MinPage&#39; => $MinPage,
          &#39;ServerPage&#39; => $ServerPage,
          &#39;PageData&#39; => $PageData,
          &#39;PageShowData&#39; => $PageShowData
        );
      }
  }

The above introduces the paging subscript generation code of the web page (PHP back-end method), including the content, I hope to be more familiar with PHP Tutorials are helpful for those who are interested.

Statement:
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