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";
}
}
}
You need to record the state of your 'category' between page refreshes, a few ideas...
- Pass your category to your pagination class and incorporate this into your 'next', 'prev' anchors etc.
- Save the category in a cookie / session and read this when you build your query.
- Don't use anchors for your 'next', 'prev' links, but have these as submit buttons in a form. Make the category a hidden field in the form, so that it is passed back as well.
If you could give session code example for the same problem that would be great.
OR
Is there any way i can append $_GET variables from previous page to next one by retrieving its name and value? (i don't know PHP functions that can do it) What i am trying is not to hard code specific $_GET variables into my pagination class, which kills the whole purpose of class itself.
Many thanks in advance,
Ayub