復制代碼 代碼如下:
$nav='';//用來保存頁數的一個變量
for ($i=1;$i<=13;$i++)
{
$nav.="<a href='index.php?page=".$i."'>第".$i."頁</a> ";
}
復制代碼 代碼如下:
$step= floor(($pageNow-1)/10)*10+1;
for ($i=$step;$i<=$step+10;$i++)
{
$nav.="<a href='index.php?page=".$i."'>第".$i."頁</a> ";
}
復制代碼 代碼如下:
<?php
/**
* $sql語句:①獲取數據②獲取總記錄數
*/
class fenyePage{
public $pageSize=5;//每頁顯示的數量-->程序員指定的
public $rowCount;//這是從數據庫中獲取的(形如SELECT COUNT(id) FROM TABLE)用來保存總共有多少條記錄
public $pageNow;//通過$_GET['page']獲取的,用來保存當前所在的頁碼
public $pageCount;//計算得到的,用來保存總共有多少頁
public $res_arr;//用來保存要顯示到頁面的數據(比如保存SELECT * FROM TABLE LIMIT 0,10 檢索的數據)
public $nav;//顯示第幾頁第幾頁的導航條
/**
* 取得當前頁面的超鏈接
*
* @author 小飛 2012/5/30
*/
public function getLink()
{
$this->nav='';
$this->pageCount=ceil(($this->rowCount/$this->pageSize));
$step= floor(($this->pageNow-1)/10)*10+1;
if ($this->pageNow>10)
{
$this->nav.=" <a href='index.php?page=".($step-1)."'> << </a> ";//整體每10頁向前翻
}
if ($this->pageNow!=1)
{
$this->nav.="<a href='index.php?page=".($this->pageNow-1)."'> 上一頁</a> ";
}
if ($this->pageNow!=1)
{
$this->nav.="<a href='index.php?page=1'>首頁</a> ";
}
for ($start=$step;$start<$step+10 && $start<=$this->pageCount;$start++)
{
$this->nav.="<a href='index.php?page=".$start."'>".$start."</a> ";
}
if ($this->pageNow!=$this->pageCount)
{
$this->nav.="<a href='index.php?page=".$this->pageCount."'>末頁</a> ";
}
if ($this->pageNow!=$this->pageCount)
{
$this->nav.=" <a href='index.php?page=".($this->pageNow+1)."'>下一頁</a>";
}
if ($this->pageCount>10 && $this->pageNow<$this->pageCount-8){
$this->nav.=" <a href='index.php?page=".($step+10)."'> >> </a>";//整體每10頁向后翻
}
$this->nav.="/共有".$this->pageCount."頁";
}
}
?>
復制代碼 代碼如下:
/**
* 根據指定的用戶id,查詢該用戶的歷史訂餐記錄
*
* @author 小飛 2012/5/30
* @param $id 用戶id
* @param $fenye 實例化的一個對象,用來處理分頁
* @todo $sql1語句 "select * from table where * limit 0,10" 該sql語句主要用來檢索數據庫中的數據,用以顯示在view層
* @todo $sql2語句 "select count(id) from table" 該sql語句用來得出總的數據量
*/
public function showorder($id=null,$fenye=null)
{
$db = $this->getAdapter();
$select=$db->select();
$select->from(array('o' => 'order'),array('o.id','o.user_id','o.user_name','o.food_name','o.food_price','o.order_time','o.order_state'));
if ($id!=null){
$select->where('o.user_id=?',$id);
}
$select->join(array('d'=>'department'),'o.dep_id = d.id','d.dep_name');
if($fenye!=null){
$select->limit($fenye->pageSize,($fenye->pageNow-1)*$fenye->pageSize);
}
$sql1=$select->__toString();
//該sql語句主要用來計算總的數據量
$sql2="SELECT COUNT(id) FROM `order`";
$fenye->res_arr=$db->fetchAll($sql1);//將要顯示的數據存儲到分頁類的$res_arr屬性當中,方便調用
$rowCount=$db->fetchAll($sql2);//將表中的總數據量保存到分頁類的rowCount屬性當中
$fenye->rowCount=$rowCount[0]['COUNT(id)'];
$fenye->getLink();
return $fenye->res_arr;
}
新聞熱點
疑難解答