6
7

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.

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

Last updated at Posted at 2015-02-09

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

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

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

■Controller(action_listメソッド)

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

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)

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; ?>

6
7
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
6
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?