Table of Contents
Yii paging component usage example analysis, yii paging example analysis
Articles you may be interested in:
Home Backend Development PHP Tutorial Example analysis of yii paging component usage, yii paging example analysis_PHP tutorial

Example analysis of yii paging component usage, yii paging example analysis_PHP tutorial

Jul 12, 2016 am 09:02 AM
yii Pagination components

Yii paging component usage example analysis, yii paging example analysis

The examples in this article describe 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));
}

Copy after login

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

The page.php code is now attached as follows:

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

Copy after login

I hope this article will be helpful to everyone’s PHP program design based on the yii framework.

Articles you may be interested in:

  • Yii’s method of obtaining the current url and domain name
  • Summary of Url production issues in the yii framework
  • Yii extension component Analysis of writing method examples
  • Principle analysis of Yii core component AssetManager
  • How YII uses url components to beautify management

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1085891.htmlTechArticleYii paging component usage example analysis, yii paging example analysis This article describes the usage of yii paging component. Share it with everyone for your reference, the details are as follows: When using this case, the paging class...
Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to install the Windows 10 old version component DirectPlay How to install the Windows 10 old version component DirectPlay Dec 28, 2023 pm 03:43 PM

Many users always encounter some problems when playing some games on win10, such as screen freezes and blurred screens. At this time, we can solve the problem by turning on the directplay function, and the operation method of the function is also Very simple. How to install directplay, the old component of win10 1. Enter "Control Panel" in the search box and open it 2. Select large icons as the viewing method 3. Find "Programs and Features" 4. Click on the left to enable or turn off win functions 5. Select the old version here Just check the box

PHP development: How to implement table data sorting and paging functions PHP development: How to implement table data sorting and paging functions Sep 20, 2023 am 11:28 AM

PHP development: How to implement table data sorting and paging functions In web development, processing large amounts of data is a common task. For tables that need to display a large amount of data, it is usually necessary to implement data sorting and paging functions to provide a good user experience and optimize system performance. This article will introduce how to use PHP to implement the sorting and paging functions of table data, and give specific code examples. The sorting function implements the sorting function in the table, allowing users to sort in ascending or descending order according to different fields. The following is an implementation form

How to implement calendar component using Vue? How to implement calendar component using Vue? Jun 25, 2023 pm 01:28 PM

Vue is a very popular front-end framework. It provides many tools and functions, such as componentization, data binding, event handling, etc., which can help developers build efficient, flexible and easy-to-maintain Web applications. In this article, I will introduce how to implement a calendar component using Vue. 1. Requirements analysis First, we need to analyze the requirements of this calendar component. A basic calendar should have the following functions: display the calendar page of the current month; support switching to the previous month or next month; support clicking on a certain day,

Angular components and their display properties: understanding non-block default values Angular components and their display properties: understanding non-block default values Mar 15, 2024 pm 04:51 PM

The default display behavior for components in the Angular framework is not for block-level elements. This design choice promotes encapsulation of component styles and encourages developers to consciously define how each component is displayed. By explicitly setting the CSS property display, the display of Angular components can be fully controlled to achieve the desired layout and responsiveness.

How to open the settings of the old version of win10 components How to open the settings of the old version of win10 components Dec 22, 2023 am 08:45 AM

Win10 old version components need to be turned on by users themselves in the settings, because many components are usually closed by default. First we need to enter the settings. The operation is very simple. Just follow the steps below. Where are the win10 old version components? Open 1. Click Start, then click "Win System" 2. Click to enter the Control Panel 3. Then click the program below 4. Click "Enable or turn off Win functions" 5. Here you can choose what you want to open

How to use JavaScript to implement table paging function? How to use JavaScript to implement table paging function? Oct 20, 2023 pm 06:19 PM

How to use JavaScript to implement table paging function? With the development of the Internet, more and more websites use tables to display data. In some cases where the amount of data is large, the data needs to be displayed in pages to improve user experience. This article will introduce how to use JavaScript to implement table paging function and provide specific code examples. 1. HTML structure First, we need to prepare an HTML structure to host tables and paging buttons. We can use &lt;tab

Detailed explanation of the principle of MyBatis paging plug-in Detailed explanation of the principle of MyBatis paging plug-in Feb 22, 2024 pm 03:42 PM

MyBatis is an excellent persistence layer framework. It supports database operations based on XML and annotations. It is simple and easy to use. It also provides a rich plug-in mechanism. Among them, the paging plug-in is one of the more frequently used plug-ins. This article will delve into the principles of the MyBatis paging plug-in and illustrate it with specific code examples. 1. Paging plug-in principle MyBatis itself does not provide native paging function, but you can use plug-ins to implement paging queries. The principle of paging plug-in is mainly to intercept MyBatis

Vue component practice: paging component development Vue component practice: paging component development Nov 24, 2023 am 08:56 AM

Vue component practice: Introduction to paging component development In web applications, the paging function is an essential component. A good paging component should be simple and clear in presentation, rich in functions, and easy to integrate and use. In this article, we will introduce how to use the Vue.js framework to develop a highly customizable paging component. We will explain in detail how to develop using Vue components through code examples. Technology stack Vue.js2.xJavaScript (ES6) HTML5 and CSS3 development environment

See all articles