Home >Backend Development >PHP Tutorial >About the usage of yii paging component

About the usage of yii paging component

不言
不言Original
2018-06-19 13:48:381492browse

This article mainly introduces the usage of yii paging component. It analyzes the calling of yii paging component and specific paging display implementation techniques in detail in the form of examples. Friends in need can refer to the following

The examples of this article are described Learned the usage of yii paging component. Share it with everyone for your reference, the details are as follows:

When used in this case, the paging class exists in the form of components in the Yii framework.

The action code is as follows:

public function actionIndex(){
  $user=User::model();
  //分页的使用
  $count=$user->count(); //获取总页数
  $pagesize=3; //每一页显示的记录条数
  $page=new Page($count,$pagesize);
  $sql="select * from {{user}} $page->limit";
  $info=$user->findAllBySql($sql);
  $show_page=$page->fpage();
  $this->renderPartial("index",array("info"=>$info,'show_page'=>$show_page));
}

On the view page, traverse info to obtain information, and output show_page to display the paging list and other information.

The page.php code is now attached as follows:

<?php
/**
 * 分页类,以组件形式存在于componnets文件中
 */
class Page {
  private $total; //数据表中总记录数
  private $listRows; //每页显示行数
  private $limit;
  private $uri;
  private $pageNum; //页数
  private $config=array(&#39;header&#39;=>"个记录", "prev"=>"上一页", "next"=>"下一页", "first"=>"首 页", "last"=>"尾 页");
  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"], &#39;?&#39;)?&#39;&#39;:"?").$pa;
   $parse=parse_url($url);
   if(isset($parse["query"])){
    parse_str($parse[&#39;query&#39;],$params);
    unset($params["page"]);
    $url=$parse[&#39;path&#39;].&#39;?&#39;.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=&#39;&#39;;
   if($this->page==1)
    $html.=&#39;&#39;;
   else
    $html.=" <a href=&#39;{$this->uri}&page=1&#39;>{$this->config["first"]}</a> ";
   return $html;
  }
  private function prev(){
      $html=&#39;&#39;;
   if($this->page==1)
    $html.=&#39;&#39;;
   else
    $html.=" <a href=&#39;{$this->uri}&page=".($this->page-1)."&#39;>{$this->config["prev"]}</a> ";
   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.=" <a href=&#39;{$this->uri}&page={$page}&#39;>{$page}</a> ";
   }
   $linkPage.=" {$this->page} ";
   for($i=1; $i<=$inum; $i++){
    $page=$this->page+$i;
    if($page<=$this->pageNum)
     $linkPage.=" <a href=&#39;{$this->uri}&page={$page}&#39;>{$page}</a> ";
    else
     break;
   }
   return $linkPage;
  }
  private function next(){
      $html=&#39;&#39;;
   if($this->page==$this->pageNum)
    $html.=&#39;&#39;;
   else
    $html.=" <a href=&#39;{$this->uri}&page=".($this->page+1)."&#39;>{$this->config["next"]}</a> ";
   return $html;
  }
  private function last(){
      $html=&#39;&#39;;
   if($this->page==$this->pageNum)
    $html.=&#39;&#39;;
   else
    $html.=" <a href=&#39;{$this->uri}&page=".($this->pageNum)."&#39;>{$this->config["last"]}</a> ";
   return $html;
  }
  private function goPage(){
   return &#39; <input type="text" onkeydown="javascript:if(event.keyCode==13){var page=(this.value>&#39;.$this->pageNum.&#39;)?&#39;.$this->pageNum.&#39;:this.value;location=\&#39;&#39;.$this->uri.&#39;&page=\&#39;+page+\&#39;\&#39;}" value="&#39;.$this->page.&#39;" style="width:25px"><input type="button" value="GO" onclick="javascript:var page=(this.previousSibling.value>&#39;.$this->pageNum.&#39;)?&#39;.$this->pageNum.&#39;:this.previousSibling.value;location=\&#39;&#39;.$this->uri.&#39;&page=\&#39;+page+\&#39;\&#39;"> &#39;;
  }
  function fpage($display=array(0,1,2,3,4,5,6,7,8)){
   $html[0]=" 共有<b>{$this->total}</b>{$this->config["header"]} ";
   $html[1]=" 每页显示<b>".($this->end()-$this->start()+1)."</b>条,本页<b>{$this->start()}-{$this->end()}</b>条 ";
   $html[2]=" <b>{$this->page}/{$this->pageNum}</b>页 ";
   $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=&#39;&#39;;
   foreach($display as $index){
    $fpage.=$html[$index];
   }
   return $fpage;
  }
}

The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

About yii using bootstrap paging style

How to implement the Yii framework to output and execute sql statements on the page And debug

How to use the Yii framework to remove the behavior bound to a component

The above is the detailed content of About the usage of yii paging component. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn