Forum Moderators: coopster
I am trying to do pagination of resultset that involves 2 GET variables, 1st one is 'page' and second is 'category'.
I get correct results when i query 1st page (user selected category + first page) which is:
/xyz/display_portfolio.php?page=1
But when i try the next page for the same query which is:
/xyz/display_portfolio.php?page=2
i lose the category part and and results are showed without any category.
But if i try writing manually:
/xyz/display_portfolio.php?page=1&category='logo' (wrote &category='logo' manually)
I get the correct results, can anyone tell me how i can solve this problem?
Here is my pagination class:
<?php
// Author: Ayub Zafar, 2009
// Paginates result set according to max limit and generates links
require_once("connection.php");
class pagination{
protected $_query;
protected $_max_results;
protected $_page_number;
protected $_last_page;
public function __construct($query, $max_results){
$this->_query = $query;
$this->_max_results = $max_results;
if(isset($_GET['page'])){
$this->_page_number = (int)$_GET['page'];
}else{
$this->_page_number = 1;
}
}
public function generate_result_set(){ // generates result set
$pagination = new connection($this->_query);
$paginationResultSet = $pagination->getResultset();
$query_rows = mysql_num_rows($paginationResultSet);
$last_page = ceil($query_rows/$this->_max_results);
$this->_last_page = $last_page; // last_page initialized
if ($this->_page_number < 1)
{
$this->_page_number = 1;
}
elseif ($this->_page_number > $last_page)
{
$this->_page_number = $last_page;
}
$max = ' LIMIT '.($this->_page_number - 1)*$this->_max_results.', '.$this->_max_results;
$this->_query .= $max;
$paginated = new connection($this->_query);
$paginatedResultSet = $paginated->getResultset();
return $paginatedResultSet;
}
public function generate_links(){
echo "<br/>";
echo "Displaying Page <b>$this->_page_number</b> of <b>$this->_last_page</b>";
echo "<br/><br/>";
$previous = $this->_page_number - 1;
$next = $this->_page_number + 1;
if ($this->_page_number == 1)
{
echo "START";
echo " ¦ ";
echo "PREV";
echo " ¦ ";
echo "<a href='{$_SERVER['PHP_SELF']}?page=$next'>NEXT</a>";
echo " ¦ ";
echo "<a href='{$_SERVER['PHP_SELF']}?page=$this->_last_page'>LAST</a>";
}
elseif($this->_page_number > 1 && $this->_page_number != $this->_last_page){
echo "<a href='{$_SERVER['PHP_SELF']}?page=1'>START</a>";
echo " ¦ ";
echo "<a href='{$_SERVER['PHP_SELF']}?page=$previous'>PREV</a>";
echo " ¦ ";
echo "<a href='{$_SERVER['PHP_SELF']}?page=$next'>NEXT</a>";
echo " ¦ ";
echo "<a href='{$_SERVER['PHP_SELF']}?page=$this->_last_page'>LAST</a>";
}
elseif($this->_page_number == $this->_last_page){
echo "<a href='{$_SERVER['PHP_SELF']}?page=1'>START</a>";
echo " ¦ ";
echo "<a href='{$_SERVER['PHP_SELF']}?page=$previous'>PREV</a>";
echo " ¦ ";
echo "NEXT";
echo " ¦ ";
echo "LAST";
}
}
}
$extra_vars .= '&category='.$cat;
....
$extra_vars.= '&referrer=' . $referrer;
Aalways use & in query strings to validate output. They still work. Pass the $extra_params to your pagination function, but make it optional in case there are none.
Then in your function,
if ($extra) { $next .= $extra; }
etc . . . for all the link variables.
For example: I just put a function in pagination class that dynamically checks for $_GET variables, if initialized, it appends it to the START, PREV, NEXT, LAST links.
I guess that would be great.
It would really vary. one example,
$whats_in_get = Array();
foreach ($_GET as $key=>$value) {
array_push($whats_in_get,$key);
}
A better idea might be to just pass $_GET to the function as $extra and parse $extra as an array. You would have to be careful to "skip" the pagination values though so "old page values" don't get tacked on to the new links.