现在的位置: 网页制作教程网站制作经验 >正文
php网上学习

Yii分页PHP实例代码

发表于2017/5/20 网站制作经验 0条评论 ⁄ 热度 809℃

本文总结了在Yii常用的一些yii分页方式与实例代码,有普通分页与ajax实现分页,希望此文章对大家会有所帮助。

第一种:CLinkPager 针对数组形式的数据分页

Controller页面:

public function actionIndex() {
 $criteria = new CDbCriteria();
 $criteria->order = 'news_id DESC';
 $criteria->condition = 'user_id = 1';
 $count = News::model()->count($criteria);
 $pages = new CPagination($count);
 $pages->pageSize = 10;
 $pages->applyLimit($criteria);
 $list = News::model()->findAll($criteria);
 $this->render('index', array('list' => $list, 'pages' => $pages));
}

View页面:

<UL>
<?php foreach ($list as $item): ?>
<LI>
<DIV class=page-header>
<?php echo $item--->news_title; ?>
</DIV>
<DIV class=content>
<?php echo $item--->news_intro; ?>
</DIV>
</LI>
<?php endforeach; ?>
</UL>
<DIV class=pagination>
<?php
$this--->widget('CLinkPager', array(
'pages' => $pages,
'selectedPageCssClass' => 'active', //当前页的class
'hiddenPageCssClass' => 'disabled', //禁用页的class
'header' => '', //分页前显示的内容
'maxButtonCount' => 10, //显示分页数量
'htmlOptions' => array('class' => ''),
'firstPageLabel' => '首页',
'nextPageLabel' => '下一页',
'prevPageLabel' => '上一页',
'lastPageLabel' => '末页',
)
);
?>
</DIV>

第二种: DAO实现分页

Controller页:

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,
));
}

View页:

<?php foreach($posts as $row):?>
<?php echo CHtml::link($row["remitdate"],array('delivery/view','remitdate'=>$row["sumrate"]));?>
<?php echo $row["sumrate"]."<br />" ?>
<?php endforeach;?>
<?php
//分页widget代码:
$this->widget('CLinkPager',array('pages'=>$pages));
?>
  • 暂无评论