PHP paging example code (can be modified and used directly)

怪我咯
Release: 2023-03-12 15:16:02
Original
1387 people have browsed it

When learning PHP, you will definitely encounter problems with operating MYSQL database, and pagination of the data in the database. Here is a small example to learn about PHP pagination. method.

There are many ways to paginate in PHP. Today we will use a small example to demonstrate this function.

The code is as follows:

$result = "<div class=\"page-num\"><ul class=\"fn-clear\">";

    // 上一页
    if ($offset>0) {
        $result .= "<li>
            <a href=\"".$url.&#39;offset=&#39;.($offset-$maxrow)."\">Prev</a>
        </li>";
    }

    $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<=$pageoffset){ 
            $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<=$max_p; $i++) {

        if ( $i == $curPage ) {
            $result .=    "<li class=\"on\"><a href=\"".$url.&#39;offset=&#39;.($i*$maxrow)."\" >$i</a></li>";
            continue;
        }

        $result .=    "<li><a href=\"".$url.&#39;offset=&#39;.(($i-1)*$maxrow)."\">$i</a></li>";

    }

    // 打印下一页
    if ( $allnums > ($offset+$maxrow) ) {
        $result .=    "<li>
            <a href=\"".$url.&#39;offset=&#39;.($offset+$maxrow)."\">Next</a>
        </li>";
    }
Copy after login


The above is the detailed content of PHP paging example code (can be modified and used directly). For more information, please follow other related articles on the PHP Chinese website!

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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!