Detailed explanation of two methods of implementing paging in Yii

高洛峰
Release: 2023-03-04 20:34:01
Original
1744 people have browsed it

There are two ways to implement paging in Yii, one is to use DAO, and the other is to implement it in widgets.

Each has its own advantages. The first one is more efficient, and the second one is more efficient. You can use the built-in table, which is more convenient.

1. DAO implements paging.

[Controller layer]

public function actionReport()
  {
    $sql = "select remitdate, sum(rate) sumrate from td_delivery 
      group by remitdate 
      order by remitdate desc";
    $criteria=new CDbCriteria();
    $result = Yii::app()->db->createCommand($sql)->query();
    $pages=new CPagination($result->rowCount);
    $pages->pageSize=2; 
    $pages->applyLimit($criteria); 
    $result=Yii::app()->db->createCommand($sql." LIMIT :offset,:limit"); 
    $result->bindValue(':offset', $pages->currentPage*$pages->pageSize); 
    $result->bindValue(':limit', $pages->pageSize); 
    $posts=$result->query();
    $this->render('report',array( 
        'posts'=>$posts, 
        'pages'=>$pages, 
    ));
  }
Copy after login

[View layer]

<?php foreach($posts as $row):?> 
<?php echo CHtml::link($row["remitdate"],array(&#39;delivery/view&#39;,&#39;remitdate&#39;=>$row["sumrate"]));?> 
<?php echo $row["sumrate"]."<br />" ?>
<?php endforeach;?>
<?php 
//分页widget代码: 
$this->widget(&#39;CLinkPager&#39;,array(&#39;pages&#39;=>$pages));
?>
Copy after login

Advantages: DAO is highly efficient; Disadvantages: the view layer needs to write some styles by itself, which is a little troublesome

2. Widget implements paging

[model layer]

/**
   * @var string attribute : 日运费 (统计用)
   * 需要对新增加的字段做个声明
   */
  public $dayrate;
 
 
  /*
   * 统计功能: 统计每日的运费
   */
  public function statistics()
  {
    $criteria = new CDbCriteria;
    $criteria->select = &#39;remitdate, sum(rate) AS dayrate&#39;;
    $criteria->group = &#39;remitdate&#39;;
     
    return new CActiveDataProvider(get_class($this), array(
      &#39;criteria&#39;=>$criteria,
      &#39;sort&#39;=>array(
        // 表头设置点击排序的字段
        &#39;attributes&#39;=>array(
          &#39;remitdate&#39;,
          &#39;dayrate&#39;=>array(
            &#39;asc&#39;=>&#39;dayrate&#39;,
            &#39;desc&#39;=>&#39;dayrate DESC&#39;,
          )
        ),
        &#39;defaultOrder&#39;=>&#39;remitdate desc&#39;,
      ),
    ));  
  }
Copy after login

[Controller layer]

/**
   * 运单统计功能:
   * 按日期统计
   */
  public function actionReport()
  {
    $model=new Delivery(&#39;statistics&#39;);
    $model->unsetAttributes(); // clear any default values
      
    $this->render(&#39;report&#39;,array(
      &#39;model&#39;=>$model,
    ));
  }
Copy after login

[View layer]

<?php $this->widget(&#39;zii.widgets.grid.CGridView&#39;, array(
  &#39;id&#39;=>&#39;delivery-grid&#39;,
  &#39;dataProvider&#39;=>$model->statistics(),
  &#39;filter&#39;=>$model,
  &#39;columns&#39;=>array(
    &#39;remitdate&#39;,
    &#39;dayrate&#39;,
    array(
      &#39;class&#39;=>&#39;CButtonColumn&#39;,
    ),
  ),
)); ?>
Copy after login

Advantages: You can use your own style; Disadvantages: Slightly less efficient .

The above is the entire content of this article. I hope it will be helpful to everyone's learning. I also hope that everyone will support the PHP Chinese website.

For more detailed explanations of the two methods of paging in Yii, please pay attention to the PHP Chinese website for related articles!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template