简体   繁体   English

如何限制一页中显示的博客数量。 Yii 1,PHP

[英]How to limit the number of blogs displayed in one page. Yii 1 , PHP

I am able to display the blogs in one page, but I want to display only 5 blogs per page and display next 5 blogs on clicking next page button and so on. 我可以在一个页面中显示博客,但是我只想在每页上显示5个博客,并在单击“下一页”按钮等时显示下5个博客。

Code in view file : 查看文件中的代码:

<?php
if (is_array($blogArticles) && count($blogArticles) > 0):
foreach ($blogArticles as $key => $blog_article):
?>

 <h3>
<a href="#" style="color: black">
<?php print $blog_article['title']; ?>
</h3><br/>
Author : <?php print $blog_article['author']; ?>
<?php print $blog_article['published_date']; ?> 
<a href="<?php print Yii::app()->createUrl("//site/blogsingle" , array("id"=>$blog_article['id']))  ?>" class="btn btn-info" role="button">Read More</a>

I also need options below 5 blogs to redirect to next pages. 我还需要5个博客以下的选项来重定向到下一页。 ex : <1><2>2<3> 例如:<1> <2> 2 <3>

Yii - ListView - define CActiveDataProvider , inside provider change pager -> pageSize to 5, pass CActiveDataProvider to view and use ListView to display it properly. YII -的ListView -定义CActiveDataProvider ,提供商改变内部pager - > pageSize 〜5,通过CActiveDataProvider查看和使用ListView以正确地显示它。 Something like: 就像是:

public function searchPosts() {

   $criteria = new CDbCriteria();

   return new CActiveDataProvider('MODELNAME',
                array(
                        'criteria'  => $criteria,
                )
            );

}

Controller: 控制器:

$model = new Post();
$dataProvider = $model->searchPosts();

return $this->render('view', ['dataProvider' => $dataProvider]);

And the view code: 和查看代码:

$this->widget('zii.widgets.CListView', array(
        'dataProvider'=>$dataProvider,
            'itemView'=>'_view',
));

Don't forget about _view file, which is single item view. 不要忘记_view文件,它是单项视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM