controller

$criteria = new CDbCriteria;

$criteria->order = ‘id asc’; //那边还足以写其余的sql语句

$count = Bankinfo::model()->count($criteria);

$pages = new CPagination($count);
$pages->pageSize = 1;
$pages->applylimit($criteria);

$bankinfos = Bankinfo::model()->findAll($criteria);

$this->render(‘index’, array(
‘bankinfos’ => $bankinfos,
‘pages’ => $pages,
));

 

在调整器:

【金沙注册送58】xml存款和储蓄数据的轻便例子,Yii框架实现数据分页及排序的事例。第一种:CListView分页 
针对对象情势的数额分页

类文件 : _class.xmldb.inc.php

view里

<div >
<?php
$this->widget(‘MyLinkPager’,array(
‘pages’=>$pages,
));
?>
</div>

 代码如下

 代码如下

复制代码 代码如下:

view里结合bootstrap          //蛋疼在只有 currentPage属性可用

<?php if($pages->pageCount > 1):?>
<div >
<ul >
<li disabled’:”; ?>”><a
‘:$this->createUrl(‘bank/index’,array(‘page’=>$pages->currentPage));?>”>«</a></li>
<?php for($i = 1;$i <= $pages->pageCount;$i++):?>
<li active’:”; ?>”><a
bank/index’,array(‘page’=>$i));?>”><?php echo $i;
?></a></li>
<?php endfor; ?>
<li disabled’:”; ?>”><a
‘:$this->createUrl(‘bank/index’,array(‘page’=>$pages->currentPage+2));?>”>»</a></li>
</ul>
</div>
<?php endif;?>


class PostController extends Controller
{
    function actionIndex()
    {
        $criteria = new CDbCriteria();
        $count=Post::model()->count($criteria);
        $pages=new CPagination($count);
 
        // elements per page
        $pages->pageSize=5;
        $pages->applyLimit($criteria);
 
        // sorting
        $sort = new CSort(‘Post’);
        $sort->attributes = array(‘id’, ‘title’,);
        $sort->applyOrder($criteria);
 
        $models = Post::model()->findAll($criteria);
 
        $this->render(‘index’, array(
            ‘models’ => $models,
            ‘pages’ => $pages,
            ‘sort’ => $sort,
        ));
    }
}

[Controller]

<?php
class xmldb extends DOMDocument{
    var $xmldb;
    public function __construct($xmldbname){
        $this->xmldb=$xmldbname;
        if(!file_exists($this->xmldb)){
            $initxmldb = “<?xml version=\”1.0\”
encoding=\”utf-8\”?><Message></Message>”;
            $this->loadXML($initxmldb);
            $this->save($this->xmldb);
        }else{
            $this->load($this->xmldb);
        }

在视图像和文字件中:

public function actionAjax() { 

    }
    public function insert_message($nickname,$message){

 代码如下

        $criteria = new CDbCriteria(); 

        $messagedom = $this->getElementsByTagName(“Message”);
        $subnode = $this->createElement(“Rows”);
        $nicknamenode = $this->createElement(“Nickname”);
        $nicknamenode ->
appendChild($this->createTextNode($nickname));
        $subnode -> appendChild($nicknamenode);
        $contentnode = $this->createElement(“Content”);
        $contentnode ->
appendChild($this->createTextNode($message));
        $subnode -> appendChild($contentnode);
        $timernode = $this->createElement(“Timer”);
        $timernode ->
appendChild($this->createTextNode(time()));
        $subnode -> appendChild($timernode);
        $messagedom->item(0)->appendChild($subnode);
        $this->saveXML();
        $this->save($this->xmldb);
    }
    function get_node_length($nodename){

<p><?php echo $sort->link(‘id’)?></p>
<p><?php echo $sort->link(‘title’)?></p>
<ol>
<?php foreach($models as
$model):?>
    <li>
        <h2><?php echo $model->id?> – <?php echo
$model->title?></h2>
    </li>
<?php endforeach?>
</ol>
 
<?php $this->widget(‘CLinkPager’, array(
    ‘pages’ => $pages,
))?>

        //$criteria->order = ‘news_id DESC’; 

        $odom = $this->getElementsByTagName($nodename);
        return $odom->length;
    }
    public function get_message_list($start,$rowscount,$pagesize){
        $end = $rowscount > $pagesize ? $start+$pagesize :
$rowscount;

一. DAO实现分页.

        $criteria->condition = ‘user_id = 1’; 

        $k = 0;
        for($i = $start; $i < $end; $i++){
            $rowslist[$k][“Nickname”] =
$this->getElementsByTagName(“Nickname”)->item($i)->nodeValue;
            $rowslist[$k][“Content”] =
$this->getElementsByTagName(“Content”)->item($i)->nodeValue;
            $rowslist[$k][“Timer”] =
$this->getElementsByTagName(“Timer”)->item($i)->nodeValue;
            $k++;
        }
        return $rowslist;
    }
    public static function
splitpage($pageall,$page=1,$urled=null,$strpage = “page”,$pageaverage =
10){
            $pageaverage -= 1;
            $page = intval($page >=1 ? $page : 1 );
            $page = $page > $pageall ? $pageall : $page;
            $startpage = $page – $pageaverage > 0 ? $page –
ceil(($pageaverage / 2)):1;
            $startpage = ($page + ceil($pageaverage/2) >
$pageall)?$pageall-$pageaverage:$startpage;

[Controller层]

  

            $startpage = $startpage > 0 ? $startpage : 1;
            $stoppage = $startpage+$pageaverage
>$pageall?$pageall:$startpage+$pageaverage;
            if(empty($urlfile)){ $urlfile=$_SERVER[“PHP_SELF”]; }
            if(!strrpos($urlfile,’?’)) $urled .= ‘?’;
            foreach($_GET as $k => $v)
            {
                $urled = ($k<>$strpage) ?
$urled.$k.’=’.urlencode($v).’&’ : $urled;
            }
            if ($page>1){
                $mess =”<a href=””.$urled.$strpage.”=1″
href=””.$urled.$strpage.”=1″>首页</a> “;
                $mess .=”<a href=””.$urled.$strpage.”=”.($page-1).””
href=””.$urled.$strpage.”=”.($page-1).””>上一页 </a>”;
            }else{
                $mess =”首页 “;
                $mess .=”上一页 “;
            }
            if($page > 9){
                $startpage = $page – 9;
            }
            $stoppage = $startpage + 9 >= $pageall ? $pageall :
$startpage + 9;
            for($i= $startpage; $i<= $stoppage ;$i++)
            {
                if($i<= $pageall && !($page==$i))
                    $mess .= “<a href=””.$urled.$strpage.”=”.$i.””
href=””.$urled.$strpage.”=”.$i.””>”.$i.”</a> “;
                else
                    $mess .= “”.$i.” “;
            }
            if ($page < $pageall){
                $mess .=” <a href=””.$urled.$strpage.”=”.($page+1).””
href=””.$urled.$strpage.”=”.($page+1).””>下一页</a>”;
                $mess .=” <a href=””.$urled.$strpage.”=”.$pageall.””
href=””.$urled.$strpage.”=”.$pageall.””>尾页</a>”;
            }else{
                $mess .=” 下一页”;
                $mess .=” 尾页”;
            }
        return $mess;
    }
}
?>

 代码如下

        $dataProvider = new CActiveDataProvider(‘News’, array( 

调用文件 : index.php

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

                    ‘pagination’ => array( 

复制代码 代码如下:

[View层]

                        ‘pageSize’ =>
Yii::app()->params[‘pagesize’], 

<?php
require(“_class.xmldb.inc.php”);
$xmldb = new xmldb(“./my_xmldb.xml”);
if(isset($_POST[“btnform1”])){
    $xmldb ->
insert_message($_POST[“nickname”],$_POST[“content”]);
}
$pagesize        = 3;
$page            = !empty($_金沙注册送58,GET[‘page’]) ? intval($_GET[‘page’]) :
1;
$rowscount        = $xmldb -> get_node_length(“Rows”);
$pagecount        = ceil($rowscount/$pagesize);
$start            = ($page-1)*$pagesize > $rowscount ? $rowscount :
($page-1)*$pagesize;
$rowslist = $xmldb ->
get_message_list($start,$rowscount,$pagesize);
?>
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.0 Transitional//EN”>
<HTML>
<HEAD>
<meta http-equiv=”content-type”
content=”text/html;charset=utf-8″>
<TITLE> New Document </TITLE>
    <style type=’text/css’><!–
        #messagelist ul li{ float:left;list-style-type:none; }

<?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));
 ?>

                        ‘pageVar’ =>
Yii::app()->params[‘pagevar’], 

–></style><style type=’text/css’
bogus=”1″>        #messagelist ul li{
float:left;list-style-type:none; }
    </style>
</HEAD>
<BODY>
    <div id=”messagelist”>
    <?php
        if(!empty($rowslist))
        {
            foreach($rowslist as $key => $value)
            {
                echo “<ul><li>”.$value[“Nickname”].”
<span>(“.date(“Y-m-d
H:i”,$value[“Timer”]).”)</span></li>”;
                echo
“<li>”.$value[“Content”].”</li></ul><br>”;
            }
        }
    ?>
        <div><?=$xmldb ->
splitpage($pagecount,$page,”,’page’,$pagesize)?></div>
    </div>
    <div>
        <form action=”” method=”post” name=”form1″>
            昵称:<input type=”text” name=”nickname”><br>
            内容:<textarea name=”content”>

优点: DAO功用高; 缺点: view层需求团结写一些样式,稍显麻烦一点

                    ), 

php操作xml的简短留言板,带分页,仅供参谋

二. widget达成分页

                    ‘criteria’ => $criteria, 

你或然感兴趣的作品:

  • 三个简练的PHP&MYSQL留言板源码
  • php简单的留言板与回复功用具体落到实处
  • php开采留言板的CRUD(增,删,改,查)操作
  • 来自杰出的造作轻巧的PHP&MYSQL留言板
  • 三个可分页的依照文本的PHP留言板源码
  • 找到一款不错的依附AJAX留言板源码(PHP版、ASP版)提供下载了
  • php制作文本式留言板
  • php完结网址留言板成效
  • PHP结合Mysql数据库完成留言板功用
  • php完毕留言板功用(会话调控)

[model层]

                )); 

 代码如下

  

/**
     * @var string attribute : 日运费 (统计用)
     * 须要对新添的字段做个评释
     */
    public $dayrate;

  

    /*
     * 总结功效: 总结每一天的运费
     */
    public function statistics()
    {
        $criteria = new CDbCriteria;
        $criteria->select = ‘remitdate, sum(rate) AS dayrate’;
        $criteria->group = ‘remitdate’;
       
        return new CActiveDataProvider(get_class($this), array(
            ‘criteria’=>$criteria,
            ‘sort’=>array(
                // 表头设置点击排序的字段
                ‘attributes’=>array(
                    ‘remitdate’,
                    ‘dayrate’=>array(
                        ‘asc’=>’dayrate’,
                        ‘desc’=>’dayrate DESC’,
                    )
                ),
                ‘defaultOrder’=>’remitdate desc’,
            ),
        ));   
    }

        $this->render(‘view’, array( 

[Controller层]

            ‘dataProvider’ => $dataProvider, 

/**
     * 运单总括功用:
     * 按日期总括
     */
    public function actionReport()
    {
        $model=new Delivery(‘statistics’);
        $model->unsetAttributes();  // clear any default values
        
        $this->render(‘report’,array(
            ‘model’=>$model,
        ));
    }

        )); 

[View层]

    }
[View]

<?php $this->widget(‘zii.widgets.grid.CGridView’, array(
    ‘id’=>’delivery-grid’,
    ‘dataProvider’=>$model->statistics(),
    ‘filter’=>$model,
    ‘columns’=>array(
        ‘remitdate’,
        ‘dayrate’,
        array(
            ‘class’=>’CButtonColumn’,
        ),
    ),
)); ?>

<!–?php 

优点: 可以行使自带的样式; 缺点: 功能略低.

$this—>widget(‘zii.widgets.CListView’, array( 

                    ‘dataProvider’ => $dataProvider, //数据 

                    ‘itemView’ => ‘_view’, //展现的沙盘 

                    ‘id’ => Yii::app()->controller->id, 

                    ‘itemsTagName’ => ‘ul’, 

                    ‘ajaxVar’ => ”, //默认为page或ajax
去掉后url更简洁 

                    ‘htmlOptions’ => array(‘class’ =>
Yii::app()->controller->id), 

                    ‘loadingCssClass’ => ‘loading’,
//默认为list-view-loading 

                    //’template’ =>
‘{summary}{sorter}{items}{pager}’,//显示的逐条 

                    //’ajaxUpdate’ => false,
//是否ajax分页 
false或分页突显的容器id 

                    //’beforeAjaxUpdate’ => ‘before_ajax_update’,  
//回调函数 在common.js里成功 

                    //’afterAjaxUpdate’ => ‘after_ajax_update’,   

                    ’emptyText’ => ‘ 

<DIV class=”alert alert-waning”> 

    暂无数据! 

</DIV> 

‘, //无数据时展现内容 

                    ‘pagerCssClass’ => ‘pagination’, //分页的class 

                    ‘pager’ => array( 

                       
‘selectedPageCssClass’ =>
‘active’, //当前页的class 

                        ‘hiddenPageCssClass’ => ‘disabled’,
//禁止使用页的class 

                        ‘header’ => ”, //分页前浮现的内容 

                        ‘maxButtonCount’ => 10, //呈现分页数量 

                        ‘htmlOptions’ => array(‘class’ => ”), 

                        ‘firstPageLabel’ => ‘首页’, 

                        ‘nextPageLabel’ => ‘下一页’, 

                        ‘prevPageLabel’ => ‘上一页’, 

                        ‘lastPageLabel’ => ‘末页’, 

                    ), 

                )); 

  

?>

第2种: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));
 ?>

亮点: DAO功效高; 缺点: view层须要自个儿写一些体裁,稍显麻烦一点

四. widget实现分页

 代码如下

[model层]

/**
* @var string attribute : 日运费 (统计用)
* 需求对新增的字段做个注脚
*/
public $dayrate;

/*
* 总括功效: 总括每一日的运费
*/
public function statistics()
{
$criteria = new CDbCriteria;
$criteria->select = ‘remitdate, sum(rate) AS dayrate’;
$criteria->group = ‘remitdate’;

return new CActiveDataProvider(get_class($this), array(
‘criteria’=>$criteria,
‘sort’=>array(
// 表头设置点击排序的字段
‘attributes’=>array(
‘remitdate’,
‘dayrate’=>array(
‘asc’=>’dayrate’,
‘desc’=>’dayrate DESC’,
)
),
‘defaultOrder’=>’remitdate desc’,
),
));
}

[Controller层]

/**
* 运单计算功用:
* 按日期计算
*/
public function actionReport()
{
$model=new Delivery(‘statistics’);
$model->unsetAttributes(); // clear any default values
 
$this->render(‘report’,array(
‘model’=>$model,
));
}

[View层]

<?php $this->widget(‘zii.widgets.grid.CGridView’, array(
‘id’=>’delivery-grid’,
‘dataProvider’=>$model->statistics(),
‘filter’=>$model,
‘columns’=>array(
‘remitdate’,
‘dayrate’,
array(
‘class’=>’CButtonColumn’,
),
),
)); ?>

可取: 能够选取自带的体制; 缺点: 效用略低.

五,Ajax分页

YII中ajax分页有种种贯彻情势,比较古板的便是在view中写JS来贯彻,大约的就是这样:

在view中js大概逻辑是那般

 代码如下

$(‘#listview .yiiPager a’).live(‘click’,function(){
        $.ajax({
            url:$(this).attr(‘href’),
            success:function(html){
                $(‘#listview’).html(html);
            }
        });
        return false;//阻止a标签
    });

然后在controller中剖断ajax须要,再使用renderPartial方法渲染局地List视图,然后局地视图会被view中的ajax

措施填充到局地刷新的div中。controller的光景逻辑是

 代码如下

if
(Yii::app()->request->isAjaxRequest)
{
$this->renderPartial(‘_comments’,array(
    ‘model’ => $model,
    ‘comments’ => $comments,//在局地视图中foreach得到每条数据
    ‘pages’ => $pages,
   ));
    Yii::app()->end();

新生察觉YII中的CListview更便利,封装了分页,foreach展现list,还协助数据排序。具体的能够在YII的API手册

中发现。使用CListview是暗许ajax分页的,使用办法如下:

 代码如下

controller中:

$criteria = new CDbCriteria();
$criteria->order = ‘`create_time` DESC’;

$dataProvider = new CActiveDataProvider(‘Comments’, array(
    ‘pagination’=>array(
     
‘pageSize’=>Yii::app()->params[‘commentsPerPage’],//设置分页条数以鲜明收取数据的条数
  ),
  ‘criteria’=>$criteria,
  ));
$this->render(‘comments’,array(
         ‘model’ => $model,
         ‘dataProvider’ => $dataProvider,
));

然后在view中:

 代码如下

<?php $this->widget(‘zii.widgets.CListView’, array(
  ‘dataProvider’=>$dataProvider,
  ‘itemView’=>’_comments’,
  //’ajaxUpdate’=> false,//那样就不会AJAX翻页
  ‘pager’ => array(//pager
的布局音讯。默以为<CODE>array(‘class’=>’CLinkPager’)</CODE>.也可以友善安插
   ‘nextPageLabel’ => ‘下一页 »’,
   ‘prevPageLabel’ => ‘« 上一页’
  ),
//在那边还是能够布置部分排序法规,具体能够查看手册
)); ?>

诸如此类就达成了Ajax分页,很实惠。

相关文章

网站地图xml地图