一覧ページなどで一定数の表示を超えた場合、ページを分けて表示するためのリンクを作成し表示させる。
データ取り出し
※$tool->SQLでSQLを実行すると想定
$results = $tool->SQL( 'SELECT count(id) FROM contact_data order by id desc' );
$results = json_decode(json_encode($results), true); // オブジェクト型から配列型へ変換
$total = $results[0]['count(id)']; // データのトータルを取得
$limit = 20; // 表示する行数
$page_num = $total / $limit; // ページ数の計算
$num = 0; // 基本は0からスタート
if(!empty($_GET['p']) and ctype_digit($_GET['p'])) {
if($_GET['p'] > 1) {
$num = $limit * ($_GET['p'] - 1);
}
}
$limit_str = " limit ".$num.", ".$limit; // limitを指定する文字を作成
$results = $tool->SQL( 'SELECT * FROM contact_data order by id desc'.$limit_str ); // 指定した位置から表示数分取得
$list_data = json_decode(json_encode($results), true);
一覧表示
<?php if($page_num > 1): ?> <?php for($i = 1; $i <= $page_num; $i++): ?> <span<?php if((empty($_GET['p']) and $i == 1 ) or (!empty($_GET['p']) and $_GET['p'] == $i)) echo " class='active'"?>><a href="?p=<?php echo $i; ?>"><?php echo $i; ?></a></span> <?php if($i < $page_num): ?> | <?php endif; ?> <?php endfor; ?> <?php endif; ?>