1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ページング用Smartyカスタムプラグイン

Last updated at Posted at 2015-10-02

smarty_plugins に放り込むだけです。

function.pagination.php
<?php

/**
 * function.pagination.php
 */
function smarty_function_pagination($params)
{
	//レコードの総数
	$intTotal = (isset($params['total']) && 0 < $params['total']) ?
		$params['total'] :
		0;

	//レコード総数がゼロのときは何も出力しない
	if (0 === $intTotal) {
		return '';
	}

	//ページあたりの表示レコード数
	$intLimit = (isset($params['limit']) && 0 < $params['limit']) ?
		$params['limit'] :
		10;

	//現在表示中のページ番号(ゼロスタート)
	$intCurrentPage = (-1 < filter_input(INPUT_GET, 'page')) ?
		filter_input(INPUT_GET, 'page') :
		0;

	//ページの最大数
	$intMaxpage = ceil($intTotal / $intLimit);

	//現在ページの前後3ページを出力
	$intStartpage = (2 < $intCurrentPage) ? $intCurrentPage - 3 : 0;
	$intEndpage = (($intStartpage + 7) < $intMaxpage) ? $intStartpage + 7 : $intMaxpage;

	//url組み立て
	$urlparams = filter_input_array(INPUT_GET);

	$items = [];

	//最初
	$urlparams['page'] = 0;
	$items[] = sprintf('<span><a href="?%s">%s</a></span>'
		, http_build_query($urlparams)
		, '最初'
	);

	//表示中のページが先頭ではない時
	if (0 < $intCurrentPage) {
		$urlparams['page'] = $intCurrentPage - 1;
		$items[] = sprintf('<span><a href="?%s">%s</a></span>'
			, http_build_query($urlparams)
			, '前へ'
		);
	}

	for ($i = $intStartpage; $i < $intEndpage; $i++) {
		$urlparams['page'] = $i;
		$items[] = sprintf('<span%s><a href="?%s">%s</a></span>'
			, ($intCurrentPage == $i) ? ' class="current"' : ''
			, http_build_query($urlparams)
			, $i + 1
		);
	}

	//表示中のページが最後ではない時
	if ($intCurrentPage < $intMaxpage) {
		$urlparams['page'] = $intCurrentPage + 1;
		$items[] = sprintf('<span><a href="?%s">%s</a></span>'
			, http_build_query($urlparams)
			, '次へ'
		);
	}

	//最後
	$urlparams['page'] = $intMaxpage - 1;
	$items[] = sprintf('<span><a href="?%s">%s</a></span>'
		, http_build_query($urlparams)
		, '最後'
	);

	return implode(PHP_EOL, $items);
}

テンプレート側からは、以下のようにして表示します。

*.tpl
{pagination total=100 limit=10}
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?