paginationとwhere句などの絞り込み検索の組み合わせ

DBなどの検索による絞り込みとpaginationの組み合わせ例です。

Controller_Item のaction_list() は絞り込みをせず表示しています。
同じくController_Itemのaction_search() は"category"というパラメータをGETパラメータで渡し、絞り込み表示を行っています。

ポイントはpaginationのuri_segmentパラメータをpageで処理しているところと、
pagination_urlに検索パラメータ(GET)を追加しているところです。


■Controller(action_listメソッド)

```php:item.php

<?php

namespace Item;

class Controller_Item extends \Controller_Base
{

	private $_classname = 'item';
	private $_perpage = 20;

	public function action_list()
	{
		$data = array();

		$data['page'] = \Input::get('page');
		$data['posts'] = Model_Item::find('all');

		$pagination = \Pagination::forge('default', array(
				'pagination_url' => \Uri::base() . $this->_classname . '/list/' . $uid,
				'wrapper' => "<div class=\"pagination\">\n\t{pagination}\n</div>\n",
				'uri_segment' => 'page',
				'total_items' =>  count($data['posts']),
				'per_page' => $this->_perpage,
				'name' => 'bootstrap'
		));

		$data['paginate'] = \Pagination::instance('default')->render();

		if (\Pagination::get('current_page') == 1)
		{
			$data['offset'] = 0;
		}
		else
		{
			$data['offset'] = ($data['page'] -1) * $this->_perpage;
		}

		$data['posts'] = Model_Item::query()
			->where('uid', $uid)
			->order_by('updated_at', 'DESC')
			->rows_offset($data['offset'])
			->rows_limit($this->_perpage)
		->get();

		$this->template->set_global('total_items', $pagination->total_items, false);
		$this->template->content = \View::forge('list', $data, false);

	}

```

■Controller(action_searchメソッド)

```php


<?php

namespace Item;

class Controller_Item extends \Controller_Base
{

	private $_classname = 'item';
	private $_perpage = 20;

	public function action_search()
	{

		$data = array();
		$data['page'] = \Input::get('page');

		if (\Input::method() == 'GET' && \Input::get('category'))
		{

			$data['posts'] = Model_Item::query()
				->where('category_code', \Input::get('category'))
				->and_where_open()
					->or_where_open()
						->or_where('title', 'LIKE', "%" . \Input::get('search') . "%")
						->or_where('keyword', 'LIKE' , "%" . \Input::get('search') . "%")
					->or_where_close()
				->and_where_close()
			->get();

			$pagination = \Pagination::forge('default', array(
				'pagination_url' => \Uri::base() . $this->_classname . '/list?category=' . \Input::get('category'),
				'wrapper' => "<div class=\"pagination\">\n\t{pagination}\n</div>\n",
				'uri_segment' => 'page',
				'total_items' =>  count($data['posts']),
				'per_page' => $this->_perpage,
				'name' => 'bootstrap'
			));

			if (\Pagination::get('current_page') == 1)
			{
				$data['offset'] = 0;
			}
			else
			{
				$data['offset'] = ($data['page'] -1) * $this->_perpage;
			}

		}
		else
		{

			$pagination = \Pagination::forge('default', array(
				'pagination_url' => \Uri::base() . $this->_classname .  '/list/',
				'wrapper' => "<div class=\"pagination\">\n\t{pagination}\n</div>\n",
				'uri_segment' => 'page',
				'total_items' => Model_Item::count(),
				'per_page' => $this->_perpage,
				'name' => 'bootstrap'
			));

			if (\Pagination::get('current_page') == 1)
			{
				$data['offset'] = 0;
			}
			else
			{
				$data['offset'] = ($data['page'] -1) * $this->_perpage;
			}

			$data['posts'] = Model_Item::query()
				->rows_offset($data['offset'])
				->rows_limit($this->_perpage)
			->get();

		}

		$data['paginate'] = \Pagination::instance('default')->render();

		$this->template->set_global('total_items', $pagination->total_items, false);
		$this->template->content = \View::forge('list', $data, false);


	}




```


View (list.php)

```php:list.php

<?php if ($posts): ?>
<?php echo isset($paginate) ? $paginate : null; ?>

<span><?php echo isset($total_items) ? "全部で" . $total_items . "件のアイテム" : null; ?></span>

(データ表示)

<?php else: ?>
<p>No Posts.</p>
<?php endif; ?>

```