复制代码 代码如下:
/**
php经典分页类
Pager 分页函数
作者:潇湘博客
<?php
class db
{
public $conn,$db,$table,$user,$host,$unm,$pwd;
public $res;
public $char;
public $linkType;
function __construct($linkType=false,$char=”gb2312″)
{
$this->linkType=$linkType;//设定连接类型
$this->char=$char;//设定连接校对字符
$this->db=DB;
$this->user=USER;
$this->host=HOST;
$this->unm=UNM;
$this->pwd=PWD;
if($this->linkType)
{
$this->conn=mysql_pconnect($this->host,$this->unm,$this->pwd)
or die(“Database connection failure”);
}
else
{
$this->conn=mysql_connect($this->host,$this->unm,$this->pwd)
or die(“Database connection failure”);
}
mysql_select_db($this->db);
mysql_query(“set names “.$this->char);
}
function query($sql,$type=”true”)
{
//$type:默认的操作。 指代select操作
$this->res=mysql_query($sql) or die(“SQL Statement error !Please
check it again”);
$row=$type?mysql_num_rows($this->res):mysql_affected_rows();
$result[“res”]=$this->res;
$result[“row”]=$row;
return $result;
}
//fetch()方法:获取所有的记录,并写入数组
function fetch($sql)
{
$res=self::query($sql);
while ($rs=mysql_fetch_array($res[“res”]))
{
$result[]=$rs;
}
return $result;
}
//获取下一条记录
function fetchNext($filed,$currenID,$table)
{
$sql=”select * from $table where $filed>$currenID limit 0,1 “;
return self::fetch($sql);
}
//获取前一条记录
function fetchPre($filed,$currenID,$table)
{
$sql=”select * from $table where $filed<$currenID limit 0,1 “;
return self::fetch($sql);
}
}
class page extends db
{
public $currentPage,$totalRecord,$totalPage,$pageSize;
public $start;
public $flag;
public $sql;
function __construct($sql,$pagesize=5,$flag=”page”)
{
$this->sql=$sql;
$this->pageSize=$pagesize;
$this->flag=$flag;//设定翻页链接标识符
$row=parent::query($sql);
$this->totalRecord=$row[“row”];
$this->totalPage=ceil($this->totalRecord/$this->pageSize);
$page=$_REQUEST[$this->flag];
if($page<0||$page==””)
{
$this->currentPage=1;
}
else
{
$page>$this->totalPage?$this->currentPage=$this->totalPage:$this->currentPage=$page;
}
$this->start=($this->currentPage-1)*$this->pageSize;
}
//显示分页列表
function show($page=10)
{
$str.='<div class=”pages_btns”><div class=”pages”>’;
$str.=”<em> “.$this->totalRecord.” </em>”;
$pre=$this->currentPage-1;
if($pre!=0)
{
$str.='<a href=?’.$this->flag.’=’.$pre.’
class=next><<</a>’;
}
if($this->currentPage>=$page)
{
if($this->totalPage-$this->currentPage<10)
{
$start=$this->currentPage-($this->currentPage%10);
$end=$this->totalPage;
}
else
{
$start=$this->currentPage-2;
$end=$start+$page-1;
}
}
else
{
$start=1;
$end=10;
}
for ($i=$start;$i<=$end;$i++)
{
if($i==$this->currentPage)
{
$str.=”<strong>”.$i.”</strong>”;
}
else
{
$str.=”<a href=?”.$this->flag.”=$i>$i</a>”;
}
}
$next=$this->currentPage+1;
$str.='<a href=?’.$this->flag.’=’.$next.’
class=next>>></a>’;
$str.=”<a href=?”.$this->flag.”=”.$this->totalPage.”
class=last>…”.$this->totalPage.”</a>”;
$str.=”<kbd><input type=’text’ name=’custompage’ size=’3′
onkeydown=”if(event.keyCode==13)
{window.location='{$_SERVER[‘PHP_SELF’]}?{$this->flag}=’+this.value;
return false;}” /></kbd>”;
$str.=”</div></div>”;
return $str;
}
}
?>
<?php
define(“HOST”,”localhost”);
define(“UNM”,”root”);
define(“PWD”,”root”);
define(“DB”,”test”);
require_once(‘db.class.php’);
$db=new db();
$sql=”select * from yy”;
$page=new page($sql,10);
$sql.=” limit $page->start,$page->pageSize”;
$rs = $db->fetch($sql);
?>
<table width=”100%” border=”0″ cellpadding=”0″ cellspacing=”1″
bgcolor=”#FF0000″>
<tr>
<td height=”25″ bgcolor=”#FFFFFF”>ID</td>
<td bgcolor=”#FFFFFF”>UNM</td>
</tr>
<?php
for($i=0;$i<count($rs);$i++)
{
?>
<tr>
<td height=”25″
bgcolor=”#FFFFFF”><?=$rs[$i][0]?></td>
<td bgcolor=”#FFFFFF”><?=$rs[$i][1]?></td>
</tr>
<?php
}
?>
<tr>
<td height=”25″ colspan=”2″
bgcolor=”#FFFFFF”><?=$page->show()?></td>
</tr>
</table>
view
plaincopy to
clipboardprint?
时间:
2009-11-26
- /**
- * 构造函数
- *
- * 如果 $source 参数是一个 TableDataGateway 对象,则 FLEA_Helper_Pager 会调用
- * 该 TDG 对象的 findCount() 和 findAll() 来确定记录总数并返回记录集。
- *
- * 如果 $source 参数是一个字符串,则假定为 SQL 语句。这时,FLEA_Helper_Pager
- * 不会自动调用计算各项分页参数。必须通过 setCount() 方法来设置作为分页计算
- * 基础的记录总数。
- *
- * 同时,如果 $source 参数为一个字符串,则不需要 $conditions 和 $sortby 参数。
- * 而且可以通过 setDBO() 方法设置要使用的数据库访问对象。否则 FLEA_Helper_Pager
- * 将尝试获取一个默认的数据库访问对象。
- *
- * @param TableDataGateway|string $source
- * @param int $currentPage
- * @param int $pageSize
- * @param mixed $conditions
- * @param string $sortby
- * @param int $basePageIndex
- *
- * @return FLEA_Helper_Pager
- */
- function FLEA_Helper_Pager(& $source, $currentPage, $pageSize = 20, $conditions = null, $sortby = null, $basePageIndex = 0)
- {
- $this->_basePageIndex = $basePageIndex;
- $this->_currentPage = $this->currentPage = $currentPage;
- $this->pageSize = $pageSize;
- if (is_object($source)) {
- $this->source =& $source;
- $this->_conditions = $conditions;
- $this->_sortby = $sortby;
- $this->totalCount = $this->count = (int)$this->source->findCount($conditions);
- $this->computingPage();
- } elseif (!emptyempty($source)) {
- $this->source = $source;
- $sql = “SELECT COUNT(*) FROM ( $source ) as _count_table”;
- $this->dbo =& FLEA::getDBO();
- $this->totalCount = $this->count = (int)$this->dbo->getOne($sql);
- $this->computingPage();
- }
-
}
/**
* 构造函数 * * 如果 $source 参数是一个 TableDataGateway 对象,则 FLEA_Helper_Pager 会调用 * 该 TDG 对象的 findCount() 和 findAll() 来确定记录总数并返回记录集。 * * 如果 $source 参数是一个字符串,则假定为 SQL 语句。这时,FLEA_Helper_Pager * 不会自动调用计算各项分页参数。必须通过 setCount() 方法来设置作为分页计算 * 基础的记录总数。 * * 同时,如果 $source 参数为一个字符串,则不需要 $conditions 和 $sortby 参数。 * 而且可以通过 setDBO() 方法设置要使用的数据库访问对象。否则 FLEA_Helper_Pager * 将尝试获取一个默认的数据库访问对象。 * * @param TableDataGateway|string $source * @param int $currentPage * @param int $pageSize * @param mixed $conditions * @param string $sortby * @param int $basePageIndex * * @return FLEA_Helper_Pager */
function FLEA_Helper_Pager(& $source, $currentPage, $pageSize = 20, $conditions = null, $sortby = null, $basePageIndex = 0)
{ $this->_basePageIndex = $basePageIndex; $this->_currentPage = $this->currentPage = $currentPage; $this->pageSize = $pageSize; if (is_object($source)) { $this->source =& $source; $this->_conditions = $conditions; $this->_sortby = $sortby; $this->totalCount = $this->count = (int)$this->source->findCount($conditions); $this->computingPage(); } elseif (!empty($source)) { $this->source = $source; $sql = "SELECT COUNT(*) FROM ( $source ) as _count_table"; $this->dbo =& FLEA::getDBO(); $this->totalCount = $this->count = (int)$this->dbo->getOne($sql); $this->computingPage(); } }
php技术群:
37304662
Pager 参数说明
$source 数据库操作类
$currentPage 当前页
$pageSize 每页显示记录数量
$conditions 查询条件
$sortby 排序方式
$basePageIndex 页码基数
使用方法:
include_once’Pager.class.php’;
$pager=new Pager();
if(isset($_GET[‘page’]))
$pager->setCurrentPage($_GET[‘page’]);
else
$pager->setCurrentPage(1);
$pager->setRecorbTotal(1000);
$pager->setBaseUri(“page.php?”);
echo $pager->execute();
实例:
**/
class Pager{
/**
*int总页数
**/
protected $pageTotal;
/**
*int上一页
**/
protected $previous;
/**
*int下一页
**/
protected $next;
/**
*int中间页起始序号
**/
protected $startPage;
/**
*int中间页终止序号
**/
protected $endPage;
/**
*int记录总数
**/
protected $recorbTotal;
/**
*int每页显示记录数
**/
protected $pageSize;
/**
*int当前显示页
**/
protected $currentPage;
/**
*string基url地址
**/
protected $baseUri;
/**
*@returnstring获取基url地址
*/
public function getBaseUri(){
return$this->baseUri;
}
- $dirname = dirname(__FILE__);
- define(‘APP_DIR’, $dirname . ‘/APP’);
- define(‘NO_LEGACY_FLEAPHP’, true);
- require($dirname.’/FleaPHP/FLEA/FLEA.php’);
- //设置缓存目录
- FLEA::setAppInf(‘internalCacheDir’,$dirname.’/_Cache’);
- //链接数据库
- $dsn = array(
- ‘driver’ => ‘mysql’,
- ‘host’ => ‘localhost’,
- ‘login’ => ‘root’,
- ‘password’ => ”,
- ‘database’ => ‘wordpress’
- );
- FLEA::setAppInf(‘dbDSN’,$dsn);
- //读取wp_posts的内容
- FLEA::loadClass(‘FLEA_Db_TableDataGateway’);
- FLEA::loadClass(‘FLEA_Helper_Pager’);
- $page_size=’10’; //每页记录数
$page = (isset($_GET[‘page’]))?(int)$_GET[‘page’]:1;
$conditions = array(
‘ss_state’=>0,
);
$pager = new
FLEA_Helper_Pager($this->_shuoshuo,$page,$page_size,$conditions,’created
DESC’,1);
$rows =$pager->findAll();
$pager->setBasePageIndex(1); // 起始页码设为1
$this->smarty->assign(‘rowset’,$rows);
$this->smarty->assign(‘newsNavbar’,
$pager->getNavbarIndexs($page, 8));
$this->smarty->assign(‘newsPager’,
$pager->getPagerData());
$url =
array(‘ctl’=>’default’,’act’=>’helpinfo&act=shuoshuo’);
$this->smarty->assign(‘url’,$url);
/**
*@returnint获取当前显示页
*/
public function getCurrentPage(){
return $this->currentPage;
}
/**
*@returnint获取每页显示记录数
*/
public function getPageSize(){
return $this->pageSize;
}
<a href=”{{url controller=$url.ctl action=$url.act
page=$newsPager.firstPageNumber}}”
title=’首页’><<</a>{{section name=page
loop=$newsNavbar}}{{if $newsNavbar[page].index ==
$newsPager.currentPage}}<b><font
color=’red’>[{{$newsNavbar[page].number}}]</font></b>{{else}}<a
href=”{{url controller=$url.ctl action=$url.act
page=$newsNavbar[page].index}}”>{{$newsNavbar[page].number}}</a>{{/if}}{{/section}}<a
href=”{{url controller=$url.ctl action=$url.act
page=$newsPager.lastPageNumber}}”
title=’末页’>>></a><!– 共有 <font
color=”Red”>{{$newsPager.count}}</font> 条记录,分为 <font
color=”Red”>{{$newsPager.pageCount}}</font> 页显示,每页
<font color=”Red”>{{$newsPager.pageSize}}</font> 条–>
相关文章
- 什么遮盖该窗口,看了多少个钟头照旧弄不出来
- 澳门新浦京8455com:’试行二个NM,失焦会使选中项高亮撤废
- 澳门新浦京8455com宏名称平时应用全部大写的字母,//使用间接输入类名来访问静态方法Counter
- //用经纬度表示的DEM起源X坐标,//最上端深度
- json格式轻松,对json格式的字符串举行解码
- 在调用getaddrinfo函数以前,个中第八个参数为何要提早设置值?
- 正文实例陈述了Yii框架 session,中组件独有在被调用的时候才会被实例化
- Traits和类组合的语义以减低复杂性的不二等秘书技定义,PHP 落成了代码复用的一个主意
- 怎么选拔PHP对图片进行base64解码输出,decode担负图片的Base64编码与解码
- 万一不对记录Computer进行操作,不对实施Computer举办其余操作