dozoqn3347 2018-05-01 11:04
浏览 83

Yii PHP将分页添加到我的网站

I would like to add pagination to my website, but I am very new to Yii and would like some advise.

This is the page that I am looking to add pagination: https://gocar2.com/newsproducts/center

As you can see I have numerous cells for each auto news, and now I am allowing only 18 news to show on the page. I would like to add pagination here so that I can browse more news.

And this is what I tried:

newscontroller.php

<?php

use yii\data\Pagination;

class NewsproductsController extends Controller
{
public function actionCentermore()
{   

    $criteria = new CDbCriteria;
    $criteria->addCondition("approvedStatus = '1'");
    $criteria->order = 'createdDate DESC';
    $product = Newsproducts::model()->findAll($criteria);

    $Pagination = new Pagination([
        'defaultPageSize' => 15,
        'totalCount' => $product->count(),
    ]);

    $products = $product->offset($Pagination->offset)
    ->limit($Pagination->limit)
    ->all();

    $this->renderPartial('centermore',compact('products', 'Pagination'));

}
}

newsview.php will render all the top menu and banners and it will render centermore.php

$this->renderPartial('centermore', compact('products', 'Pagination'));

centermore.php (view of the news cells), I then added the LinkPager widget at the bottom.

LinkPager::widget(['Pagination' => $Pagination])

And of course, there is an error, Class LinkPager cannot be found. Can anyone give me some advise how to solve this/implement paging?

  • 写回答

1条回答 默认 最新

  • dongtui0650 2018-05-01 11:17
    关注

    In Yii2 pagination is usually handled via DataProvider, have a look at the:
    Yii2 Guide: Data Providers, especially ActiveDataProvider:

    use yii\data\ActiveDataProvider;
    
    $query = Post::find()->where(['status' => 1]);
    
    $provider = new ActiveDataProvider([
        'query' => $query,
        'pagination' => [
            'pageSize' => 10,
        ],
        'sort' => [
            'defaultOrder' => [
                'created_at' => SORT_DESC,
                'title' => SORT_ASC, 
            ]
        ],
    ]);
    
    // returns an array of Post objects
    $posts = $provider->getModels();
    
    评论

报告相同问题?

悬赏问题

  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错