成都創(chuàng)新互聯(lián)專注于企業(yè)全網(wǎng)營(yíng)銷推廣、網(wǎng)站重做改版、南譙網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、html5、購(gòu)物商城網(wǎng)站建設(shè)、集團(tuán)公司官網(wǎng)建設(shè)、外貿(mào)營(yíng)銷網(wǎng)站建設(shè)、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁(yè)設(shè)計(jì)等建站業(yè)務(wù),價(jià)格優(yōu)惠性價(jià)比高,為南譙等各大城市提供網(wǎng)站開(kāi)發(fā)制作服務(wù)。
class Page {
private $total; //數(shù)據(jù)表中總記錄數(shù)
private $listRows; //每頁(yè)顯示行數(shù)
private $limit;
private $uri;
private $pageNum; //頁(yè)數(shù)
private $config=array('header'=>"個(gè)記錄", "prev"=>"上一頁(yè)", "next"=>"下一頁(yè)", "first"=>"首 頁(yè)", "last"=>"尾 頁(yè)");
private $listNum=8;
/*
* $total
* $listRows
*/
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;
//echo $url;
$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;
}
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(){
$html ='';
if($this->page==1)
$html.='';
else
$html.=" {$this->config["first"]} ";
return $html;
}
private function prev(){
$html ='';
if($this->page==1)
$html.='';
else
$html.=" {$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.=" {$page} ";
}
$linkPage.=" {$this->page} ";
for($i=1; $i<=$inum; $i++){
$page=$this->page+$i;
if($page<=$this->pageNum)
$linkPage.=" {$page} ";
else
break;
}
return $linkPage;
}
private function next(){
if($this->page==$this->pageNum)
$html='';
else
$html=" {$this->config["next"]} ";
return $html;
}
private function last(){
if($this->page==$this->pageNum)
$html ='';
else
$html =" {$this->config["last"]} ";
return $html;
}
private function goPage(){
return ' '.$this->pageNum.')?'.$this->pageNum.':this.previousSibling.value;location=\''.$this->uri.'&page=\'+page+\'\'"> ';
}
function fpage($display=array(0,1,2,3,4,5,6,7,8)){
$html[0]=" 共有{$this->total}{$this->config["header"]} ";
$html[1]=" 每頁(yè)顯示".($this->end()-$this->start()+1)."條,本頁(yè){$this->start()}-{$this->end()}條 ";
$html[2]=" {$this->page}/{$this->pageNum}頁(yè) ";
$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;
}
}
?>
/*如何使用分頁(yè)類
1.加載include 分頁(yè)類
2.實(shí)例化對(duì)象
3.查找類里面的構(gòu)造方法 傳參數(shù) 一般只傳分頁(yè)總數(shù) 每頁(yè)顯示數(shù) 第三個(gè)或者默認(rèn)參數(shù)可以不管用 特別強(qiáng)調(diào) 分頁(yè)總數(shù)是我們從數(shù)據(jù)庫(kù)里查詢表的數(shù)據(jù)總數(shù) 每頁(yè)顯示數(shù)使我們自己設(shè)定的
4.我們根據(jù)分頁(yè)的limit數(shù) 一般都是成員屬性 加入到我們查詢的語(yǔ)句后面
$sql = "select * from message";
$sql = "select * from message {$page->limit}";
5.顯示分頁(yè)效果 直接輸出就行
6.實(shí)例化后調(diào)用 $page->fpage();顯示結(jié)果
*/
//引入分頁(yè)類
public function actionpage(){
$count=Yii::app()->db->createCommand('select count(*) num from user')->queryAll();
$page = new Page($count[0]['num'],7);
$countd=Yii::app()->db->createCommand('select * from user '.$page->limit)->queryAll();
$this->render('page',array('countd'=>$countd,'fpage'=>$page->fpage()));
}
?>