Home > php教程 > PHP源码 > 一个PHP分页代码,函数形式,返回起始页

一个PHP分页代码,函数形式,返回起始页

PHP中文网
Release: 2016-05-25 17:14:41
Original
1038 people have browsed it

PHP分页代码
调用: $start=show_page($query,$page,$link,$offset);
其中: $start为起始记录
        $query为全记录检索SQL语句
        $page为当前页码
        $link为传递的页面参数
        $offset为每页显示记录数

<?php
//======function.php=====
//======分页函数==========
function show_page($query,$page,$link,$offset)
{
 $db = new mysql();
 $result = $db->query($query);
 $Page_size = $offset; //取得每页显示的最大订单数
 $count = $db->affected_rows($result); //总订单数
 $page_count = ceil($count/$Page_size); //计算得出总页数

$init=1;
$page_len=7;
$max_p=$page_count;
$pages=$page_count;

//判断当前页码
$page=(empty($page)||$page<0)?1:$page;
$start=$Page_size*($page-1);

//分页功能代码
 $page_len = ($page_len%2)?$page_len:$pagelen+1; //页码个数
 $pageoffset = ($page_len-1)/2; //页码个数左右偏移量

 $key="共 $count 条 ";
 $key.="$page/$pages  ";//第几页,共几页
if($page!=1){
 $key.="<a href="".$_SERVER[&#39;PHP_SELF&#39;]."?page=1&$link">第一页</a>";//第一页
 $key.="<a href="".$_SERVER[&#39;PHP_SELF&#39;]."?page=".($page-1)."&$link">上一页</a>";//上一页
}
else
{
 $key.="第一页";//第一页
$key.="上一页";//上一页
}
if($pages>$page_len)
{
//如果当前页小于等于左偏移
if($page<=$pageoffset){
$init=1;
 $max_p = $page_len;
}
 else //如果当前页大于左偏移
{
//如果当前页码右偏移超出最大分页数
if($page+$pageoffset>=$pages+1){
 $init = $pages-$page_len+1;
}
else
{
//左右偏移都存在时的计算
 $init = $page-$pageoffset;
 $max_p = $page+$pageoffset;
}
}
}
for($i=$init;$i<=$max_p;$i++)
{
if($i==$page){$key.=&#39; [&#39;.$i.&#39;]&#39;;}
 else {$key.="<a href="".$_SERVER[&#39;PHP_SELF&#39;]."?page=".$i."&$link">".$i."</a>";}
}
if($page!=$pages)
{
 $key.="<a href="".$_SERVER[&#39;PHP_SELF&#39;]."?page=".($page+1)."&$link">下一页</a>";//下一页
 $key.="<a href="".$_SERVER[&#39;PHP_SELF&#39;]."?page=".$pages."&$link">最后一页</a>";//最后一页
}
else
{
 $key.="下一页"; //下一页
$key.="最后一页";//最后一页
}
 echo"$key<BR><BR>";
 return $start;
}
?>
Copy after login
<?php
include("../include/mysql.class.php");
include("../include/function.php");
 $query ="select * from $table order by id desc";
$offset=($_GET[offset]!=&#39;&#39;)?$_GET[offset]:$offset;
$start=show_page($query,$_GET[page],"offset=$offset",$offset);

$db->close();
?>
Copy after login
Related labels:
php
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
Latest Articles by Author
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template