Brett
Brett

Reputation: 20049

Yii2: Pagination and LinkPager - starts at page 0?

I have just built a pagination system using Pagination class as well as the LinkPager but noticed that page numeration starts from 0 for some reason?

It's rather annoying as the pagination generated links "Page 1" to Page 1, which is actually Page 2.

Here is my generalised code:

$sql = $this->db->createCommand("SELECT COUNT(*) FROM some_table WHERE some_id=:some_id");
$sql->bindValue(':some_id', $this->some_id);
$count = $sql->queryScalar();

$dataProvider = new SqlDataProvider([
    'sql' => 'SELECT t.*, a.image_path, a.upload_date FROM some_table AS t LEFT JOIN other_table AS a ON t.user_id=a.user_id WHERE t.some_id=:some_id',
    'params' => [':some_id' => $this->some_id],
    'totalCount' => $count,
    'sort' => [
        //.............
    ],
    'pagination' => [
        'pageSize' => $this->per_page,
        'page' => $this->page,
        'pageSizeLimit' => [5,100],
        'pageSizeParam' => 'per_page',
        'totalCount' => $count,
    ],
]);

This code also runs on class initialization:

$this->page = ($this->page) ? $this->page : 1;

It appends the below to the query with the above code:

LIMIT 25 OFFSET 25

If I change it to:

$this->page = ($this->page) ? $this->page : 0;

Then it just appends:

LIMIT 25

Is there any way to make it start @ page 1 instead?

Upvotes: 1

Views: 2494

Answers (1)

Neeraj Kumar
Neeraj Kumar

Reputation: 1058

remove

'page' => $this->page, 

from your pagination array this will use the default page option of framework for pagination.

Upvotes: 2

Related Questions