複製代碼 代碼如下:
<?php
class Page
{
private $pageSize;//每一頁多少行
private $absolutePage;//當前頁
private $pageCount;//總頁碼數
private $totalNum;//總行數
private $prePage;//上一頁
private $nextPage; //下一頁
private $sqlStr;
private $baseUrl; //不帶GET參數的URL get傳輸網址
function __construct($pagesize,$absolutepage,$baseurl,$sqlStr)
{
$this->absolutePage = $absolutepage;
$this->pageSize = $pagesize;
$this->baseUrl = $baseurl;
$this->sqlStr = $sqlStr;
}
//顯示主體內容
public function listinfo()
{
$result = DBHelper::ExecuteCommand($this->sqlStr);
// $result = mysql_query($this->sqlStr,$conn);
$this->totalNum = mysql_num_rows($result);//取處結果集的所有行數
$this->pageCount = (int)(($this->totalNum -1)/$this->pageSize)+1; //計算出一共有多少頁
if($this->absolutePage == "" || !is_numeric($this->absolutePage))//如果是頁面第一次載入那麼absolutePage的值一定是空的這時我們認為使用者是第一次訪問就給他看第一頁(當然你如果想給他看第5頁也可以)
{
$this->absolutePage = 1;//給他看第一頁
}
if($this->absolutePage > $this->pageCount)//如果請求的下一頁的頁碼超過了總頁數我們就顯示最後一頁
{
$this->absolutePage = $this->pageCount;//顯示最後一頁
}
if ($this->totalNum > 1 && $this->absolutePage > 1) //如果滿足顯示上一頁的條件我們就讓上一頁的頁碼變數的值等於當前頁(absolutePage)減一
{
$this->prePage = $this->absolutePage -1;
}
if($this->absolutePage >= 1 && $this->absolutePage < $this->pageCount)//在當前頁至少是1並且不大於總頁數時給下一頁變數賦值為當前頁加1
{
$this->nextPage = $this->absolutePage + 1;
}
if (mysql_data_seek($result,($this->absolutePage -1) * $this->pageSize))//決定從結果集的哪個位置開始檢索資料//決定從結果集的哪個位置開始檢索資料 mysql_data_seek指向下一行
{
include("goodsTemplate.php");
for ($i = 0; $i < $this->pageSize; $i++)
{
if(($info = mysql_fetch_array($result)))
{
$name = $info['GoodsName'];
$tupian = $info['GoodsPhopo'];
$id = $info['GoodsId'];
$price = $info['GoodsPrice'];
$url = 'detail.php?id='.$id;
$items = str_replace(array('{name}','{tupian}','{url}','{price}','{id}'),array($name,$tupian,$url,$price,$id),$goodsContent);//str_replace替換兩個數組 第二個替換第一個數組,$goodsContent參數是要替換的對象
$cishu++;
echo $items;
if ($cishu % 4 == 0)
{
?>
</tr><tr>
<?php
}
}
}
}
}
//分頁動作函數
public function toPage()
{
include('toPageTemplate.php');
if($this->totalNum > 1 && $this->absolutePage > 1)
{
$this->prePage = $this->absolutePage - 1;
}
if($this->absolutePage >= 1 && $this->absolutePage < $this->pageCount)
{
$this->nextPage = $this->absolutePage + 1;
}
$preUrl = $this->baseUrl."?absolutePage=$this->prePage";
$nextUrl = $this->baseUrl."?absolutePage=$this->nextPage";
$items = str_replace(array('{totalNum}','{absolutePage}','{pageCount}','{preUrl}','{nextUrl}'),array($this->totalNum,$this->absolutePage,$this->pageCount,$preUrl,$nextUrl),$toPage);
echo $items;
}
}
?>