Page 1 of 1

splitting results

Posted: Wed Nov 08, 2006 10:48 am
by murlopaz
I have a bunch of faq questions and answers in my database.
I want to split the search results (while searching the faq).
Say if the user gets more than 5 results I want him to be able to see just the first 5 on the first page.
And the next 5 on the next page and so on...

How would I accomplish this task?

Posted: Wed Nov 08, 2006 10:55 am
by Luke
It's called pagination... search for it on these forums... there have been probably hundreds of topics about this. See if you can dig up some code, or write your own, and if you need any help, just ask! :D

Posted: Wed Nov 08, 2006 10:56 am
by murlopaz
nice thanks :)
I was looking for that word.

Posted: Wed Nov 08, 2006 11:08 am
by Luke
Hey... I just found this paginator class I wrote a while back... it's not the best code in the world, but I wrote it pretty quickly.

Code: Select all

class MC2_Paginator{
	private $_page = 1;
	private $_total;
	private $_url;
	public function __construct($page, $total, $url){
		if(!empty($page)) $this->_page = $page;
		$this->_total = $total;
		$this->_url = $url;
	}
	public function toHtml(){
		if($this->_total == 1) return "";
		$paginate = "<div class='pagination'>";
		if($this->_page > 1){
			if($this->_total >= 2 && $this->_page > 2){
				$paginate .= ' <a href="' . $this->_url . '&page=1"><<</a> ';
			}
			$page = $this->_page - 1;
			$paginate .= ' <a href="' . $this->_url . '&page=' . $page . '"><</a> ';
		}
		for($i=1; $i <= $this->_total; $i++){
			$paginate .= $this->_page == $i ? " " . $i . " " : ' <a href="' . $this->_url . '&page=' . $i . '">' . $i . '</a>';
		}
		if($this->_page < $this->_total){
			$page = $this->_page + 1;
			$paginate .=  ' <a href="' . $this->_url . '&page=' . $page . '">></a> ';
			if(($this->_page + 1) < $this->_total){
				$paginate .= ' <a href="' . $this->_url . '&page=' . $this->_total . '">>></a> ';
			}
		}
		$paginate .= "</div>";
		return $paginate;
	}
}
Example Usage:

Code: Select all

<?php
		// Build pagination

                // Always remember to filter input, but for the sake of simplicity, I'll skip that part
		$showpage = isset($_GET['page']) ? $_GET['page'] : 0; 

		// If the configuration file specifies how many records to show per page, use that number, otherwise display 10 per page
		$perpage = isset($this->config->view->perpage) ? $this->config->view->perpage : 10; 

		// This basically resolves to this query: SELECT COUNT(*) AS count FROM `my_table` WHERE `date` > '2006-04-23 10:00:00'
		// It is used to find the total number of 
		$conditions = "`date` > '2006-04-23 10:00:00'";
		$total_count = $Member->count($conditions);
	
		// Find total amount of pages
		$total_pages = ceil($total_count / $perpage);
		$start = $showpage ? $showpage * $perpage - $perpage : 0;
		
		// This basically resolves to this query: SELECT * FROM `my_table` WHERE `date` > '2006-04-23 10:00:00'
		// It is used to find the total number of 
		$members = $Member->find($conditions . ' LIMIT ' . $start . ',' . $perpage);

		$Paginator = new MC2_Paginator($showpage, $total_pages, $this->config->view->baseurl . '/member/search');
		$this->view->assign('pagination', $Paginator->toHtml());
?>