Home > Backend Development > PHP Tutorial > PHP encapsulates the paging function to implement text paging and number paging, _PHP tutorial

PHP encapsulates the paging function to implement text paging and number paging, _PHP tutorial

WBOY
Release: 2016-07-13 10:16:12
Original
831 people have browsed it

PHP encapsulates the paging function to implement text paging and number paging,

Recently, paging is used in projects. The paging function is a frequently used function, so it is encapsulated in the form of a function.

// 分页分装<br>/**<br>*  $pageType  分页类型 1是数字分页 2是文本分页<br>* 可以将$pageTotal,$page,$total等数据作为参数传递,或者在paging作为全局变量(推荐)<br>*/<br>function paging($pageType)<br>{<br>  global $pageTotal,$page,$total;<br>  if($pageType == 1)<br>  {<br>    echo '<div id="pagenum">';<br>      echo'<ul>';<br>        for($i=0; $i < $pageTotal; $i++)<br />        { <br />          if($page == ($i+1))<br />          {<br />            echo '<li><a href="blogfriends.php&#63;page='.($i+1).'">'.($i+1).'</a></li>';<br>          }<br>          else<br>          {<br>            echo '<li><a href="blogfriends.php&#63;page='.($i+1).'">'.($i+1).'</a></li>';<br>          }<br>        }<br>      echo'</ul>';<br>    echo'</div>';<br><br>  }<br>  else if($pageType == 2)<br>  {<br>    echo '<div id="pagetext">';<br>      echo '<ul>';<br>        echo '<li>'.$page.'/'.$pageTotal.'页 | </li>';<br>        echo '<li>共有<strong>'.$total .'</strong>个会员 | </li>';<br>          // 第一页<br>          if($page == 1)<br>          {<br>            echo '<li>首页 | </li>';<br>            echo '<li>上一页 | </li>';<br>          }<br>          else<br>          {<br>            // $_SERVER["SCRIPT_NAME"]获取当前的脚本名字,方便移植<br>            // 也可以自定义常量,常量值和脚本文件名一致<br>            echo '<li><a href="'.$_SERVER["SCRIPT_NAME"].'">首页 </a>| </li>';<br>            echo '<li><a href="'.$_SERVER["SCRIPT_NAME"].'&#63;page='.($page - 1).'">上一页 </a>| </li>';<br>          }<br>          // 最后一页<br>          if($page == $pageTotal)<br>          {<br>            echo '<li>下一页 | </li>';<br>            echo '<li>尾页 | </li>';<br>          }<br>          else<br>          {<br>            echo '<li><a href="'.$_SERVER["SCRIPT_NAME"].'&#63;page='.($page + 1).'">下一页 </a>| </li>';<br>            echo '<li><a href="'.$_SERVER["SCRIPT_NAME"].'&#63;page='.($pageTotal).'">尾页 </a>| </li>';<br>          }    <br>      echo '</ul>';<br>    echo '</div>';<br>  }<br>}
Copy after login

Parameter explanation:

$pageTotal is the total number of pages, $page is the current page, $total is the total number of data obtained from the database;

For simplicity, all parameters are encapsulated

// 分页参数分装<br>/**<br>*  $sql  可以 获取数据总数的一个sql语句<br>* $size 每一页显示条数<br>*/<br>function pageParam($sql,$size)<br>{<br>  // 将所有涉及的参数设置全局变量<br>  // $pagestart 某一页从哪里开始<br>  // $total 总记录数  $page 某一页 $pageTotal 总页数<br>  global $pagestart,$pagesize,$total,$page,$pageTotal;<br>  $pagesize = $size;<br>  // 获取数据总数<br>  $total = mysql_num_rows(queryDB($sql));<br><br>  // 错误处理,先判断是否存在<br>  if(isset($_GET['page']))<br>  {<br>    // 具体某一页<br>    $page = $_GET['page'];<br>    // 判断是否为空(0是空)/小于0/是否是数字<br>    if(empty($page) || $page < 0 || !is_numeric($page))<br />    {<br />      $page = 1;<br />    }<br />    else<br />    {<br />      $page = intval($page); //取整,防止小数出现<br />    }<br />    <br />  }<br />  else<br />  {<br />    // 初始化显示第1页<br />    $page = 1;<br />  }<br /><br />  // 数据库清零<br />  if($total == 0)<br />  {<br />    // 设置为1<br />    $pageTotal = 1;<br />  }<br />  else<br />  {<br />    // 分页的总页数(进一取整处理)<br />    $pageTotal = ceil($total / $pagesize);<br />  }<br /><br />  // 页数大于总页码$total<br />  if($page > $pageTotal)<br>  {<br>    $page = $pageTotal;<br>  }<br>  // 当页从某一条记录开始<br>  $pagestart = ($page - 1) * $pagesize;<br>}
Copy after login

Parameter explanation:

$pagestart is when the page starts from a certain record, $pagesize is the number of records displayed on each page

In use, call pageParam first, then call paging

/**<br>* 第一个 可以 获取数据总数的一个sql语句<br>* 第二个 每一页显示条数<br>*/<br>pageParam("select userid from user",2);
Copy after login
<&#63;php <br />  // 分页类型 1是数字分页 2是文本分页<br />   paging(2);<br />&#63;>  
Copy after login

The calling position is selected according to the specific situation, and the text is paginated as follows:

<&#63;php <br />    // 分页类型 1是数字分页 2是文本分页<br />    paging(1);<br /> &#63;>  
Copy after login

Numbers are paginated as follows:

The style can be adjusted by yourself.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/898294.htmlTechArticlePHP encapsulates the paging function to implement text paging and digital paging. Recently, paging is used in projects. The paging function is a frequently used function, so it is blocked in the form of a function...
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