php分页类
just
posted @ 2009年5月20日 20:46
in php
, 1275 阅读
来源:http://topic.csdn.net/u/20090518/09/ce1ae38a-3267-47f0-b150-0a87a5f6ff08.html
<?php
/**
* Page navigation class
* bailing
* http://gudai.cnblogs.com/
* 共有记录:147条 每页显示5条 页次:1/15 首页 上一页 下一页 尾页
* 2009
*/
class pageNav {
var $pageLink="{{link}}_{{page}}.html";
var $selectPageLink= "'{{link}}_'+this.value+'.html'";
var $page = null;
var $totalPages = null;
function pageNav( $page , $limit , $total )
{
$this->totalPages = 0;
if($total>0)
$this->totalPages = intval(ceil($total/$limit));
$this->page = min($this->totalPages , $page);
$this->limit = $limit;
$this->total = $total;
}
function getHtml()
{
$html = '';
$total_pages = $this->totalPages;
$prep = max(1,($this->page-1));
$nextp = min($this->totalPages,($this->page+1));
$firstLink = str_replace("{{page}}","1",$this->pageLink);
$lastLink = str_replace("{{page}}",$this->totalPages,$this->pageLink);
$preLink = str_replace("{{page}}",$prep,$this->pageLink);
$nextLink = str_replace("{{page}}",$nextp,$this->pageLink);
if ( $this->page> 1) {
$html .= "\n <a href='".$firstLink."' > 首页 </a>";
$html .= "\n <a href='".$preLink."' > 上一页 </a>";
} else {
$html .= "\n <span class=\"pagenav\">首页 </span>";
$html .= "\n <span class=\"pagenav\">上一页 </span>";
}
if ( $this->page < $this->totalPages ) {
$end_page = $total_pages;
$html .= "\n <a href='".$nextLink."' > 下一页 </a>";
$html .= "\n <a href='".$lastLink."' > 尾页 </a>";
} else {
$html .= "\n <span class=\"pagenav\">下一页 </span>";
$html .= "\n <span class=\"pagenav\">尾页 </span>";
}
$html = "共有记录:".$this->total."条 每页显示".$this->limit."条 页次:".$this->page."/".$this->totalPages . $html ;
return $html;
}
}//end class
?>
$limit = 12;
$baseLink = "/comment/".$id."_{{page}}.html";
$this->tpl->set( "baseLink" , $baseLink );
list( $total , $limitstart , $limit ) = pagerNew( $this->DB , "tab_ly" , $where , $tpl , $limit , $p , $baseLink );
//调用方法,大家能看懂吧。
function pagerNew( $DB , $table , $where , &$tpl , $limit=30 , $p=1 , $baseLink="/index.php" , $total=0 )
{
LoadAPI("pageNav3");
if(!$p)
{
$p = intval($_GET['p']);
if($p <=0)$p=1;
}
//计算开始条数
$limitstart = ($p-1)*$limit;
//统计共有记录数
if(!$total)
{
$q2 = "SELECT count(*) FROM ".$table." $where" ;
$DB->setQuery( $q2 );
$total = $DB->loadResult();
}
if($limitstart>=$total )
{
$limitstart = $total-$total%$limit;
if($total>0)
die("参数错误,无此页码");
}
if( $limitstart <0 )$limitstart=0;
$tpl->set( "baseLink" , $baseLink );
if($total>0)
{
$nav = new pageNav( $p , $limit , $total );
$nav->pageLink = $baseLink;
$pageLinks = $nav->getHtml();
$thispage = max($nav->page,1);
$tpl->set( "pageLinks",$pageLinks);
$tpl->set( "p",$thispage);
$tpl->set( "totalPages",$nav->totalPages);
}
else
{
$tpl->set( "p",1);
$tpl->set( "totalPages",1);
}
$tpl->set( "total",$total);
//echo $limitstart;
return array( $total , $limitstart , $limit );
}
/**
* Page navigation class
* bailing
* http://gudai.cnblogs.com/
* 共有记录:147条 每页显示5条 页次:1/15 首页 上一页 下一页 尾页
* 2009
*/
class pageNav {
var $pageLink="{{link}}_{{page}}.html";
var $selectPageLink= "'{{link}}_'+this.value+'.html'";
var $page = null;
var $totalPages = null;
function pageNav( $page , $limit , $total )
{
$this->totalPages = 0;
if($total>0)
$this->totalPages = intval(ceil($total/$limit));
$this->page = min($this->totalPages , $page);
$this->limit = $limit;
$this->total = $total;
}
function getHtml()
{
$html = '';
$total_pages = $this->totalPages;
$prep = max(1,($this->page-1));
$nextp = min($this->totalPages,($this->page+1));
$firstLink = str_replace("{{page}}","1",$this->pageLink);
$lastLink = str_replace("{{page}}",$this->totalPages,$this->pageLink);
$preLink = str_replace("{{page}}",$prep,$this->pageLink);
$nextLink = str_replace("{{page}}",$nextp,$this->pageLink);
if ( $this->page> 1) {
$html .= "\n <a href='".$firstLink."' > 首页 </a>";
$html .= "\n <a href='".$preLink."' > 上一页 </a>";
} else {
$html .= "\n <span class=\"pagenav\">首页 </span>";
$html .= "\n <span class=\"pagenav\">上一页 </span>";
}
if ( $this->page < $this->totalPages ) {
$end_page = $total_pages;
$html .= "\n <a href='".$nextLink."' > 下一页 </a>";
$html .= "\n <a href='".$lastLink."' > 尾页 </a>";
} else {
$html .= "\n <span class=\"pagenav\">下一页 </span>";
$html .= "\n <span class=\"pagenav\">尾页 </span>";
}
$html = "共有记录:".$this->total."条 每页显示".$this->limit."条 页次:".$this->page."/".$this->totalPages . $html ;
return $html;
}
}//end class
?>
$limit = 12;
$baseLink = "/comment/".$id."_{{page}}.html";
$this->tpl->set( "baseLink" , $baseLink );
list( $total , $limitstart , $limit ) = pagerNew( $this->DB , "tab_ly" , $where , $tpl , $limit , $p , $baseLink );
//调用方法,大家能看懂吧。
function pagerNew( $DB , $table , $where , &$tpl , $limit=30 , $p=1 , $baseLink="/index.php" , $total=0 )
{
LoadAPI("pageNav3");
if(!$p)
{
$p = intval($_GET['p']);
if($p <=0)$p=1;
}
//计算开始条数
$limitstart = ($p-1)*$limit;
//统计共有记录数
if(!$total)
{
$q2 = "SELECT count(*) FROM ".$table." $where" ;
$DB->setQuery( $q2 );
$total = $DB->loadResult();
}
if($limitstart>=$total )
{
$limitstart = $total-$total%$limit;
if($total>0)
die("参数错误,无此页码");
}
if( $limitstart <0 )$limitstart=0;
$tpl->set( "baseLink" , $baseLink );
if($total>0)
{
$nav = new pageNav( $p , $limit , $total );
$nav->pageLink = $baseLink;
$pageLinks = $nav->getHtml();
$thispage = max($nav->page,1);
$tpl->set( "pageLinks",$pageLinks);
$tpl->set( "p",$thispage);
$tpl->set( "totalPages",$nav->totalPages);
}
else
{
$tpl->set( "p",1);
$tpl->set( "totalPages",1);
}
$tpl->set( "total",$total);
//echo $limitstart;
return array( $total , $limitstart , $limit );
}
2018年12月29日 12:32
The information you share is great. I appreciate your sharing. Thank you very much