Yii framework paging implementation method details, yii framework paging details

Source: Internet
Author: User
Tags php foreach

Yii framework paging implementation method details, yii framework paging details

This article describes how to implement the Yii framework paging. We will share this with you for your reference. The details are as follows:

The framework used by the next company is yii. After reading the relevant tutorials over the past few days, I wrote the paging tutorials today and finally integrated the tp pages into yii, I personally think that yii paging is not as easy as tp.

On the homepage, create an Auth. php model file in the models Directory, which is written

class Auth extends CActiveRecord {  public static function model($className = __CLASS__) {    return parent::model($className);  }  public function tableName() {    return '{{auth}}';  }}

Create the control file IndexController. php In the controllers directory, and write

class IndexController extends Controller {  public function actionList() {    $criteria = new CDbCriteria();    $criteria->order = 'a_id desc';    $count = Auth::model()->count($criteria);    $page = new CPagination($count);    $page->pageSize = 10;    $page->applyLimit($criteria);    $auth = Auth::model()->findAll($criteria);    $this->renderPartial('auth', array('page' => $page, 'list' => $auth));  }  public function actionList1() {    $p = isset($_GET['page']) ? $_GET['page'] : 0;    $criteria = new CDbCriteria();    $criteria->select = "a_id,a_nickname";    $criteria->condition='';    $criteria->limit = 10;    $criteria->offset = $p == 0 ? 0 : (($p-1)*10);    $criteria->order = 'a_id desc';    $auth = Auth::model()->findAll($criteria);    $count = Auth::model()->count($criteria);    $page = new CPagination($count);    $page->pageSize = 10;    $page->applyLimit($criteria);    $this->renderPartial('auth', array('page' => $page, 'list' => $auth));  }}

Here, actionList and actionList1 are two methods of writing $ criteria.

Add the index DIRECTORY to the views directory, and add the auth. php file to the index directory.

<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/> <div class = "blogList"> <ul> <? Php foreach ($ list as $ key =>$ value) {?> <Li> <a> <? Php echo $ value ['A _ nickname'];?> </A> </li> <? Php }?> </Ul> </div> <div id = "page"> <? Php $ this-> widget ('clinkpager', array ('firstpagelabel '=> 'homepage', 'lastpagelabel' => 'last page', 'prevpagelabel '=> 'previous page ', 'nextpagelabel '=> 'Next page', 'pages' => $ page, 'maxbuttoncount' => 13,);?> </Div>

The above is the yii built-in writing method. Here we introduce the tp paging class and make a simple change. The steps are as follows:

First, set the AjaxPage of tp. class. php and Page. class. move php to protected/components under the yii project directory, and change the file name distribution to AjaxPage. php and Page. php: enter two files respectively, and remove the C method from them.

$this->varPage = C('VAR_PAGE') ? C('VAR_PAGE') : 'p' ;

Change

$this->varPage = 'p' ;

After modification, the two files do not need to be introduced, because yii is automatically loaded at startup. You can see in the configuration of protected/config. php/main. php

// autoloading model and component classes  'import'=>array(    'application.models.*',    'application.components.*',  ),

Second, in protected/config. create a common in the php/directory. PHP file, this file is put in some public functions of the project, friends familiar with tp should know that tp also has public function files, it is very useful, here for reference, yii should also have it, not found yet. Write in this file

// Obtain the list function getListByPage ($ model, $ select = '*', $ condition = '', $ limit = 10, $ order = '', $ p = '', $ ajax = 0) {// initialization parameter $ _ GET ['P'] = isset ($ _ GET ['P'])? Intval ($ _ GET ['P']): 1; $ limit = intval ($ limit)> 0? Intval ($ limit): 10; if ($ p) {$ _ GET ['P'] = intval ($ p )? Intval ($ p): 1 ;}$ criteria = new CDbCriteria (); $ count = $ model-> count ($ criteria); if ($ ajax) {$ Page = new AjaxPage ($ count, $ limit);} else {$ Page = new Page ($ count, $ limit );} $ result ['page'] = trim ($ page-> show (); $ criteria-> select = $ select; $ criteria-> condition = $ condition; $ criteria-> limit = $ Page-> listRows; $ criteria-> offset = $ Page-> firstRow; $ criteria-> order = $ order; $ list = $ model-> findAll ($ criteria); $ result ['LIST'] = $ list; return $ result ;}

This file can be introduced, or it cannot be loaded. You can introduce it in index. php of the project's entry file. The Code is as follows:

require_once(dirname($config) . '/common.php');

Finally, this method is called in the paging area in indexController. php.

public function actionPage() {    $model = Auth::model();    $info = getListByPage($model);    $this->renderPartial('page', array('info' => $info));}

This method is encapsulated. When calling paging, you only need to pass several parameters, which is simple and quick. On the page. php page, call

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><div class="blogList"><ul>  <?php foreach($info['list'] as $key=>$value){ ?>  <li>    <a><?php echo $value['a_nickname'];?></a>  </li>  <?php } ?></ul></div><div id="page"><?php  echo $info['page'];?></div>

At the same time, you can use findAll to query by page. The Code is as follows:

Function getListByPage ($ model, $ select = '*', $ condition = '', $ limit = 10, $ order ='', $ p = '', $ ajax = 0) {if (! $ Model) {return array ();} // initialization parameter $ _ GET ['P'] = isset ($ _ GET ['P'])? Intval ($ _ GET ['P']): 1; $ limit = intval ($ limit)> 0? Intval ($ limit): 10; if ($ p) {$ _ GET ['P'] = intval ($ p )? Intval ($ p): 1 ;}$ count = $ model-> count (); if ($ ajax) {$ Page = new AjaxPage ($ count, $ limit );} else {$ Page = new Page ($ count, $ limit);} $ result ['page'] = trim ($ page-> show ()); $ result ['LIST'] = $ model-> findAll (array ('select' => $ select, 'condition '=> $ condition, 'order' => $ order, 'limit' => $ Page-> listRows, 'offset' => $ Page-> firstRow,); return $ result ;}

Summary:

After having gone through the ci and tp frameworks, the progress of yii is much faster. To master a framework, I personally think that it is nothing more than mastering the mvc usage rules. At the model layer, I call the database method to obtain data. At the controller layer, I call the model layer data for logical processing and then pass it to the view layer, at the same time, I understand the framework's template operations, form operations, paging operations, file upload operations, cookie and session operations, and url calls. These skills are almost the same after the integration of the project, after understanding the common operations, let's look at the source code, compare and summarize the differences and commonalities between the frameworks, and sublimate our own technology. In the future, when using common development, let's talk about it and get a considerable salary.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.