【发布时间】:2017-09-13 05:58:06
【问题描述】:
我进行了分页,但所有帖子(我有 8 个)都显示在每个页面上。我想在每页上显示其中的 4 个。我的错误在哪里?我认为问题出在count($posts),因为在我查看的其他帖子中,他们使用的是$posts->count(),但这种方法对我不起作用。感谢每一个建议!
控制器动作:
public function actionIndex()
{
$searchModel = new PostSearch();
$postModel = new \app\models\Post();
$userModel = new \app\models\BlogUser();
$posts = $postModel::find()->orderBy(['post_id' => SORT_DESC])->all();
$pagination = new Pagination(['totalCount' => count($posts), 'pageSize' => 4]);
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
'postModel' => $postModel,
'userModel' => $userModel,
'posts' => $posts,
'pagination' => $pagination
]);
}
查看 index.php:
<div class="content">
<div class="row">
<?php Pjax::begin(['id' => 'posts']) ?>
<div class="col-md-12">
<?php
foreach ($posts as $post)
{
$id = $post->user_id;
$user = $userModel::find()->where(['id' => $id])->one();
echo "<div class='col-md-6 blog-post'>
<div class='col-md-3 post-prof-img'>"
. Html::img('../images/' . $user->image, ['alt' => 'image', 'class' => 'tall img-circle']) .
"<p class='text-center title-par'><strong><em> $user->username </em></strong></p>
<p class='text-center'> $post->date_create</p>
</div>
<div class='col-md-9 post-cont'><p>"
. Html::a('Click for more!',['view', 'id' => $post->post_id]) .
"</p><label class='text-center'> $post->title : </label>
<div class='fade-post'>
$post->content
</div>
</div>
</div>";
}
?>
</div>
<?php Pjax::end() ?>
</div>
</div>
<div class="row">
<div class="col-md-3">
<?= Html::a('Create Post', ['create'], ['class' => 'btn btn-primary']) ?>
<?= Html::submitButton('Search', ['class' => 'btn search-button', 'style' => 'color: blue']) ?>
</div>
</div>
【问题讨论】:
标签: php pagination yii2