Skip to content

Commit 6676e24

Browse files
committed
import SimplePagination class
0 parents  commit 6676e24

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

SimplePagination.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
/**
3+
* 単純なページネーションを扱うクラス
4+
*
5+
* - ページ番号は 1 以上
6+
* - リンクには前のページと次のページしか使わない
7+
*
8+
* @package SimplePagination
9+
* @author tatsuya.tsuruoka@gmail.com
10+
*/
11+
class SimplePagination
12+
{
13+
public $current; // 現在のページ番号
14+
public $prev; // ひとつ前のページ番号
15+
public $next; // ひとつ次のページ番号
16+
public $count; // 1ページに何件表示するか
17+
public $start_index; // 何件目から表示するか(1オリジン)
18+
public $is_last_page; // 最終ページかどうか
19+
20+
public function __construct($current, $count)
21+
{
22+
$this->current = $current;
23+
$this->count = $count;
24+
$this->prev = max($current - 1, 0);
25+
$this->next = $current + 1;
26+
$this->start_index = ($current - 1) * $count + 1;
27+
}
28+
29+
/**
30+
* 最終ページかどうか判定する
31+
*
32+
* このメソッドの前に、アイテムを1ページに表示する件数 + 1 個取得しておく。
33+
* 取得できたアイテムの数が1ページに表示する件数以下だったとき、
34+
* 現在のページが最終ページであることが分かる。
35+
*
36+
* 最終ページではなかったときは余分に取得したアイテムを破棄する。
37+
*
38+
* @param array &$items 取得できたアイテムの数から最終ページかどうか判断する。
39+
* @return void
40+
*/
41+
public function checkLastPage(array &$items)
42+
{
43+
if (count($items) <= $this->count) {
44+
$this->is_last_page = true;
45+
} else {
46+
$this->is_last_page = false;
47+
array_pop($items);
48+
}
49+
}
50+
}

0 commit comments

Comments
 (0)