Need help with pagination

Discussion in 'PHP' started by rob7676, Mar 7, 2012.

  1. #1
    I recently went through a tutorial to create pagination to my results page, how ever it only displays all row of the table. I tried editing it to display the results of the enter form data, but am having trouble getting it. The first page shows the correct results, but when you hit "next" it returns page 2 of all table data.

    results.php
    // the current page number ($current_page)
    $page = !empty($_GET['page']) ? (int)$_GET['page'] : 1;
    
    // records per page ($per_page)
    $per_page = 5;
    
    // total record count ($total_count)
    $_SESSION['keyword'] = $_POST['keyword'];
    $keyword = $_SESSION['keyword'];
    $total_count = Document::count_results($keyword);
    
    // Find all documents
    // Use pagination instead
    // $documents = Document::find_all();
    
    $pagination = new Pagination($page, $per_page, $total_count);
    
    // Instead of finding all records, just find records for current page.
    
    $sql  = "SELECT * FROM public ";
    $sql  .= "WHERE Filename LIKE '%" . $keyword . "%' ";
    $sql .= "LIMIT {$per_page} ";
    $sql .= "OFFSET {$pagination->offset()}";
    
    // $documents = Document::find_by_sql($sql);
    $documents = Document::FindByKeyword($keyword, $sql);
    PHP:
    document class
    
    	public static function find_by_sql($sql = "") {
    		global $database;
    		$result = $database->query($sql);
    		$object_array = array();
    		while($row = $database->fetch_array($result)) {
    			$object_array[] = self::instantiate($row);
    		}
    		return $object_array;
    	}
    	public static function FindByKeyword($keyword = "", $sql) {
    		global $database;
    		// $sql = "SELECT * FROM public WHERE Filename LIKE '%" . $keyword . "%' LIMIT {$limit} OFFSET {$offset}";
    		$result = self::find_by_sql($sql);
    		return $result;
    	}
    PHP:
    pagination class
    class Pagination {
    
    	public $current_page;
    	public $per_page;
    	public $total_count;
    	
    	public function __construct($page = 1, $per_page = 10, $total_count = 0) {
    		$this->current_page = (int)$page;
    		$this->per_page     = (int)$per_page;
    		$this->total_count  = (int)$total_count;
    	}
    	
    	public function offset() {
    		return ($this->current_page - 1) * $this->per_page;
    	}
    	
    	public function total_pages() {
    		return ceil($this->total_count/$this->per_page);
    	}
    	
    	public function previous_page() {
    		return $this->current_page - 1;
    	}
    	
    	public function next_page() {
    		return $this->current_page + 1;
    	}
    	
    	public function has_previous_page() {
    		return $this->previous_page() >= 1 ? true : false;
    	}
    	
    	public function has_next_page() {
    		return $this->next_page() <= $this->total_pages() ? true : false;
    	}
    	
    	
    }
    PHP:
    Any help would be appreciated.
     
    rob7676, Mar 7, 2012 IP