Home > Backend Development > PHP Tutorial > PHP function implements paging including text paging and number paging, _PHP tutorial

PHP function implements paging including text paging and number paging, _PHP tutorial

WBOY
Release: 2016-07-13 10:16:28
Original
897 people have browsed it

PHP function implements paging including 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.

// 分页分装
/**
* $pageType 分页类型 1是数字分页 2是文本分页
* 可以将$pageTotal,$page,$total等数据作为参数传递,或者在paging作为全局变量(推荐)
*/
function paging($pageType)
{
global $pageTotal,$page,$total;
if($pageType == 1)
{
echo '<div id="pagenum">';
echo'<ul>';
for($i=0; $i < $pageTotal; $i++)
{ 
if($page == ($i+1))
{
echo '<li><a href="blogfriends.php&#63;page='.($i+1).'" class="selected">'.($i+1).'</a></li>';
}
else
{
echo '<li><a href="blogfriends.php&#63;page='.($i+1).'">'.($i+1).'</a></li>';
}
}
echo'</ul>';
echo'</div>';

}
else if($pageType == 2)
{
echo '<div id="pagetext">';
echo '<ul>';
echo '<li>'.$page.'/'.$pageTotal.'页 | </li>';
echo '<li>共有<strong>'.$total .'</strong>个会员 | </li>';
// 第一页
if($page == 1)
{
echo '<li>首页 | </li>';
echo '<li>上一页 | </li>';
}
else
{
// $_SERVER["SCRIPT_NAME"]获取当前的脚本名字,方便移植
// 也可以自定义常量,常量值和脚本文件名一致
echo '<li><a href="'.$_SERVER["SCRIPT_NAME"].'">首页 </a>| </li>';
echo '<li><a href="'.$_SERVER["SCRIPT_NAME"].'&#63;page='.($page - 1).'">上一页 </a>| </li>';
}
// 最后一页
if($page == $pageTotal)
{
echo '<li>下一页 | </li>';
echo '<li>尾页 | </li>';
}
else
{
echo '<li><a href="'.$_SERVER["SCRIPT_NAME"].'&#63;page='.($page + 1).'">下一页 </a>| </li>';
echo '<li><a href="'.$_SERVER["SCRIPT_NAME"].'&#63;page='.($pageTotal).'">尾页 </a>| </li>';
} 
echo '</ul>';
echo '</div>';
}
}

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

// 分页参数分装
/**
* $sql 可以 获取数据总数的一个sql语句
* $size 每一页显示条数
*/
function pageParam($sql,$size)
{
// 将所有涉及的参数设置全局变量
// $pagestart 某一页从哪里开始
// $total 总记录数 $page 某一页 $pageTotal 总页数
global $pagestart,$pagesize,$total,$page,$pageTotal;
$pagesize = $size;
// 获取数据总数
$total = mysql_num_rows(queryDB($sql));

// 错误处理,先判断是否存在
if(isset($_GET['page']))
{
// 具体某一页
$page = $_GET['page'];
// 判断是否为空(0是空)/小于0/是否是数字
if(empty($page) || $page < 0 || !is_numeric($page))
{
$page = 1;
}
else
{
$page = intval($page); //取整,防止小数出现
}

}
else
{
// 初始化显示第1页
$page = 1;
}

// 数据库清零
if($total == 0)
{
// 设置为1
$pageTotal = 1;
}
else
{
// 分页的总页数(进一取整处理)
$pageTotal = ceil($total / $pagesize);
}

// 页数大于总页码$total
if($page > $pageTotal)
{
$page = $pageTotal;
}
// 当页从某一条记录开始
$pagestart = ($page - 1) * $pagesize;
}

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

/**
* 第一个 可以 获取数据总数的一个sql语句
* 第二个 每一页显示条数
*/
pageParam("select userid from user",2);

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

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

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

Copy after login

Numbers are paginated as follows:

The style can be adjusted by yourself.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/898283.htmlTechArticlePHP function implements paging including 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...
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