0

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.

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. ex : <1><2>2<3>

Mukesh
  • 13
  • 5
  • Check this out https://stackoverflow.com/questions/22145259/simple-pagination-for-foreach-loop – jhanel ayson Jun 23 '17 at 06:54
  • No, dont do this. It's stupid idea to inventing circle again. He's using framework with build in functions like that. – Yupik Jun 23 '17 at 08:23

1 Answers1

0

Yii - ListView - define CActiveDataProvider, inside provider change pager -> pageSize to 5, pass CActiveDataProvider to view and use ListView to display it properly. 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.

Yupik
  • 4,932
  • 1
  • 12
  • 26