哪位幫忙看下這個分頁類如何調用,小弟我說的是在查詢語句下

來源:互聯網
上載者:User
哪位幫忙看下這個分頁類怎麼調用,我說的是在查詢語句下
網上找到一個比較好看的分頁類,儘管作者已經說明怎麼調用了,但是對我來說還是不太明白,主要是在sql語句裡面怎麼調用,下面是分頁類代碼:

/**
* 可以靈活定製的分頁類
*
* 可以定製的選項包括,分頁連結顯示效果,當前頁碼連結按鈕的樣式,URL中擷取分頁值的名字,可以隨意帶自己的參數
*
* 使用方法:
* 1、初始化類的時候需要傳入參數,類型為數組。
* array(
* (必填)'totalRows'=>'100', 需要顯示的資料的總條數;
* (必填)'pageSize'=>'2', 每頁需要顯示的代碼數量;
* (必填)'currentPage'=>$_GET['p'], 當前頁碼,預設可以通過$_GET['p']擷取,其中名字p可以定製
* (必填)'baseUrl'=>'/welcome?id=3',你當前頁面的連結地址,比如為http://www.xxx.com/test.php(或者/test.php),如果後面帶有參數則可以為http://www.xxx.com/test?id=8
* (選填,預設為3)'offset'=>'3', 當前頁碼的左右位移量,比如當前頁碼為5,則在5的左右各顯示幾個數字連結,預設為3個,則效果為2,3,4,5,6,7,8
* (選填,預設為p)'pageString'=>'p',通過$_GET['p']擷取當前頁碼時候的名字,預設為p
* (選填,預設為here)'className'=>'here',當前頁碼連結按鈕的樣式,預設樣式名為here,所以你可以這樣寫css樣式.here{background:#FF4500;} )
*
* 2、可以使用的方法。
* A、初始化類後,需要調用pagination([$style = '1'][,$output=TRUE])方法產生分頁連結
* 關於參數的說明:
* @param $style (預設為 1,可不填寫) :擷取連結全部組件,即 首頁+上一頁+數字連結+下一頁+尾頁
* @param $style == 2 :僅擷取數字連結
* @param $style == 3 :僅擷取上一頁+下一頁
* @param $style == 4 :僅擷取上一頁+數字連結+下一頁,(不包含首尾頁)
*
* @param $output (預設為TRUE),返回分頁連結
* @param $output 為FALSE時,直接輸出分頁連結
*
* B、getCurrentPage()擷取當前頁碼,經過真偽判斷後的,防止使用者自行輸入錯誤,比如http://www.xxx.com/test?p=-100;此時通過此方法擷取當前頁碼為1
*
* C、pageAmount()擷取總的頁碼數量
*
* @author 星空幻穎
* @link http://blog.sina.com.cn/yanyinghq
*
*/
class Page
{
private $pageSize; //您的網站每一頁顯示的列表條數
private $totalRows; //通過資料庫查詢返回的總的記錄條數
private $url; //基準URL
private $pageAmount; //頁碼的總數
private $currentPage; //當前的頁碼
private $offset = 4; //頁碼位移量
private $pageString = 'p'; //頁碼在URL中的名字
private $classHere = 'class="here"'; //當前頁連結的class樣式類名,預設為here

//初始化當前頁碼,記錄總條數,每頁多少條記錄
public function __construct($param)
{
$this->pageSize = $param['pageSize'];
$this->totalRows = $param['totalRows'];
$this->url = $param['baseUrl'];
$this->offset = !empty($param['offset'])?$param['offset']:$this->offset;
$this->pageString = !empty($param['pageString'])?$param['pageString']:$this->pageString;
$this->classHere = !empty($param['className'])?$param['className']:$this->classHere;
$this->currentPage = (int)$param['currentPage'];
}

/**
* 建立分頁連結
*
* @param $style 預設為 1 :擷取連結全部組件
* @param $style == 2 :僅擷取數字連結
* @param $style == 3 :僅擷取上一頁,下一頁
* @param $style == 4 :僅擷取上一頁、下一頁、數字連結,不包含首尾頁
*
* @param $output 為TRUE時,返回分頁連結
* @param $output 為FALSE時,直接輸出分頁連結
*
*/
public function pagination($style = '1',$output=TRUE)
{
$this->baseUrl();
$this->pageAmount();
$this->currentPage();

//擷取全部組件
if($style == '1')
{
$page = $this->indexPage().$this->prevPage().$this->pageNumber().$this->nextPage().$this->endPage();
}
else if($style == '2')
{
//擷取純數字連結
$page = $this->pageNumber();
}
else if($style == '3')
{
//只擷取上一頁下一頁
$page = $this->prevPage().$this->nextPage();
}
else if($style =='4')
{
//上一頁、下一頁、數字連結
$page = $this->prevPage().$this->pageNumber().$this->nextPage();
}

if($output)
{
return $page;
}
else
{
echo $page;
}
}

/**
* 擷取當前頁碼
*
* @return 當前頁碼,經過真偽判斷的
*/
public function getCurrentPage()
{
$this->pageAmount();
$this->currentPage();
return $this->currentPage;
}

/**
* 計算出所有的頁數
*
* 可以類外面直接調用此方法返回頁碼總數
*
* @return 頁碼的總數
*/
public function pageAmount()
{
$this->pageAmount = ceil( $this->totalRows / $this->pageSize);
if($this->pageAmount <= 0)
{
$this->pageAmount = '1';
}
return $this->pageAmount;
}

/**
* 判斷基準連結是否攜帶參數
*
* 基準連結為使用者提交當前頁碼連結
*
* 如果攜帶參數,則在連結之後加&p=
*
* 如果不攜帶參數,則直接加?p=
*/
private function baseUrl()
{
if(preg_match('/\?/', $this->url))
{
$this->url = $this->url.'&'.$this->pageString.'=';
}
else
{
$this->url = $this->url.'?'.$this->pageString.'=';
}
}

/**
* 驗證當前頁碼的真偽性
*
* 如果當前頁碼小於1或者沒有,則預設當前頁碼為1
*
* 如果當前頁碼大於頁碼總數,則預設當前頁碼為頁碼總數
*
*/
private function currentPage()
{
if($this->currentPage < 1 || !$this->currentPage)
{
$this->currentPage = 1;
}
else if(($this->currentPage > $this->pageAmount))
{
$this->currentPage = $this->pageAmount;
}
}

/**
* 首頁連結
*/
private function indexPage()
{
if($this->currentPage == 1) return;
return 'url.'1">首頁';
}

/**
* 尾頁連結
*/
private function endPage()
{
if($this->currentPage == $this->pageAmount) return;
return 'url.$this->pageAmount.'">尾頁';
}

/**
* 上一頁
*/
private function prevPage()
{
if($this->currentPage == 1) return;
return 'url.( $this->currentPage - 1 ).'">上一頁';
}

/**
* 下一頁
*/
private function nextPage()
{
if($this->currentPage == $this->pageAmount) return;
return 'url.( $this->currentPage + 1 ).'">下一頁';
}

/**
* 中間頁碼的連結
*
*/
private function pageNumber()
{
$left ="";
$right = "";

//如果總記錄的條數“大於”所有連結的數量時候
if($this->pageAmount > ($this->offset * 2 + 1))
{
//當前頁碼距離首頁的距離
$leftNum = $this->currentPage - 1;

//當前頁碼距離尾頁的距離
$rightNum = $this->pageAmount - $this->currentPage;

//噹噹前頁碼距離首頁距離不足位移量offset時候,在右邊補齊缺少的小方塊
if( $leftNum < $this->offset)
{
//左邊的連結
for($i = $leftNum; $i >= 1 ; $i--)
{
$left .= 'url.( $this->currentPage - $i ).'">'.( $this->currentPage - $i ).'';
}

//右邊的連結
for($j = 1; $j <= ($this->offset * 2 - $leftNum); $j++)
{
$right .= 'url.( $this->currentPage + $j ).'">'.( $this->currentPage + $j ).'';
}
}
else if($rightNum < $this->offset)
{
//左邊的連結
for($i = ($this->offset * 2 - $rightNum); $i >= 1 ; $i--)
{
$left .= 'url.( $this->currentPage - $i ).'">'.( $this->currentPage - $i ).'';
}

//右邊的連結
for($j = 1; $j <= $rightNum; $j++)
{
$right .= 'url.( $this->currentPage + $j ).'">'.( $this->currentPage + $j ).'';
}
}
else
{
//當前連結左邊的連結
for($i = $this->offset; $i >= 1 ; $i--)
{
$left .= 'url.( $this->currentPage - $i ).'">'.( $this->currentPage - $i ).'';
}

//當前連結右邊的連結
for($j = 1; $j <= $this->offset; $j++)
{
$right .= 'url.( $this->currentPage + $j ).'">'.( $this->currentPage + $j ).'';
}
}

return $left.'url.$this->currentPage.'" class="here">'.$this->currentPage.''.$right;
}
else
{
$allLink='';
//當頁碼總數小於需要顯示的連結數量時候,則全部顯示出來
for($j = 1; $j <= $this->pageAmount; $j++)
{
$allLink.='url.$j.'" '.($j == $this->currentPage?$this->classHere:'').'>'.$j.'';
}
return $allLink;
}
}

}

------解決思路----------------------
這個類只負責分頁條的產生
唯一可能與資料庫查詢有關的是參數數組 $param['totalRows'] 項
因為待分頁的總行數是查詢得到的,所以這個查詢應在 $param 賦值之前完成

------解決思路----------------------
樓主也夠懶的了,我這裡有個給你,樣式都寫好了

class Page {
private $total; //資料表中總記錄數
private $listRows; //每頁顯示行數
private $limit;
private $uri;
private $pageNum; //頁數
private $config=array('header'=>"個記錄", "prev"=>"上一頁", "next"=>"下一頁", "first"=>"首 頁", "last"=>"尾 頁");
private $listNum=8;
/* $total 11. * $listRows 12. */
public function __construct($total, $listRows=10, $pa=""){
$this->total=$total;
$this->listRows=$listRows;
$this->uri=$this->getUri($pa);
$this->page=!empty($_GET["page"]) ? $_GET["page"] : 1;
$this->pageNum=ceil($this->total/$this->listRows);
$this->limit=$this->setLimit();
}
private function setLimit(){
return "Limit ".($this->page-1)*$this->listRows.", {$this->listRows}";
}
private function getUri($pa){
$url=$_SERVER["REQUEST_URI"].(strpos($_SERVER["REQUEST_URI"], '?')?'':"?").$pa;
$parse=parse_url($url);
if(isset($parse["query"])){
parse_str($parse['query'],$params);
unset($params["page"]);
$url=$parse['path'].'?'.http_build_query($params);
}
return $url;
}
public function __get($args){
if($args=="limit")
return $this->limit;
else
return null;
}
private function start(){
if($this->total==0)
return 0;
else
return ($this->page-1)*$this->listRows+1;
}
private function end(){
return min($this->page*$this->listRows,$this->total);
}
private function first(){
if($this->page==1)
$html.='';
else
$html.="
  • uri}&page=1'>{$this->config["first"]}
  • ";
    return $html;
    }
    private function prev(){
    if($this->page==1)
    $html.='';
    else
    $html.="
  • uri}&page=".($this->page-1)."'>{$this->config["prev"]}
  • ";
    return $html;
    }
    private function pageList(){
    $linkPage="";
    $inum=floor($this->listNum/2);
    for($i=$inum; $i>=1; $i--){
    $page=$this->page-$i;
    if($page<1)
    continue;
    $linkPage.="
  • uri}&page={$page}'>{$page}
  • ";
    }
    $linkPage.="
  • {$this->page}
  • ";
    for($i=1; $i<=$inum; $i++){
    $page=$this->page+$i;
    if($page<=$this->pageNum)
    $linkPage.="
  • uri}&page={$page}'>{$page}
  • ";
    else
    break;
    }
    return $linkPage;
    }
    private function next(){
    if($this->page==$this->pageNum)
    $html.='';
    else
    $html.="
  • uri}&page=".($this->page+1)."'>{$this->config["next"]}
  • ";
    return $html;
    }
    private function last(){
    if($this->page==$this->pageNum)
    $html.='';
    else
    $html.="
  • uri}&page=".($this->pageNum)."'>{$this->config["last"]}
  • ";
    return $html;
    }
    /*private function goPage(){
    return ' ';
    }*/
    private function goPage(){
    return ' ';
    }
    function fpage($display=array(0,1,2,3,4,5,6,7,8)){
    $html[0]="共有{$this->total}{$this->config["header"]} ";
    $html[1]=" 每頁顯示".($this->end()-$this->start()+1)."條,本頁{$this->start()}-{$this->end()}條 ";
    $html[2]=" {$this->page}/{$this->pageNum}頁 ";
    $html[3]=$this->first();
    $html[4]=$this->prev();
    $html[5]=$this->pageList();
    $html[6]=$this->next();
    $html[7]=$this->last();
    $html[8]=$this->goPage();
    $fpage='';
    foreach($display as $index){
    $fpage.=$html[$index];
    }
    return $fpage;
    }
    }
    ?>

    具體調用方法:
    $num=15; //每頁顯示數
    $page=new page($total,$num);
    $result=$db->query("select * from ".$db->table('article').$where." order by addtime desc {$page->limit}");
    迴圈
    fpage(array(3,4,5,6,7,0,1,2,8));?> //php分頁類調用,這些數字可以去類檔案裡面看
      fpage(array(3,4,5,6,7,8));?>
    這是我調用的
    CSS樣式檔案
    .fenye li{float:left; font-family:Arial, Helvetica, sans-serif; margin-left:6px; display:inline; line-height:30px;}
    .fenye a{display:block;height:30px; min-width:30px; text-align:center; font-size:14px; border:1px solid #d6d6d6; float:left; margin-left:3px; padding:3px 5px;line-height:30px;text-decoration:none;color:#666;}
    .fenye a:hover{background:#FF4500;border-color:#FF4500; color:#FFF;}
    .fenye a.here{background:#FF4500;border-color:#FF4500; color:#FFF;}
    .fenye .sel{background:#E5EDF2; color:#333; font-weight:bold; border:1px #C2D5E3 solid; padding:0 12px; border-radius:4px}
    下面上:
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.