Forum Moderators: coopster

Message Too Old, No Replies

Getting $ GET variable name/key

How to retrieve GET variable name

         

ayub_zar

5:59 am on Dec 19, 2009 (gmt 0)

10+ Year Member



hello everyone :D,

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";
}
}
}

ayub_zar

9:01 am on Dec 19, 2009 (gmt 0)

10+ Year Member



sorry about the double post, my apologies

StoutFiles

3:27 pm on Dec 19, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm not seeing $_GET['category'] anywhere in your code, making the variable useless if you don't get it out of the URL.

ayub_zar

5:36 pm on Dec 19, 2009 (gmt 0)

10+ Year Member



true but i want dont want to hard code 'category' GET variable into my pagination class, this way class will not remain an INTERFACE, it will become CONCRETE IMPLEMENTATION. Is there any other way around? i mean some best practice to handle GET variables, dynamically retrieving their name/keys and values, and use them in more general way?

rocknbil

10:22 pm on Dec 19, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Add a third param to your function, call it "$extra" or something. In your scripts,

$extra_vars .= '&amp;category='.$cat;
....
$extra_vars.= '&amp;referrer=' . $referrer;

Aalways use &amp; 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.

ayub_zar

5:47 am on Dec 20, 2009 (gmt 0)

10+ Year Member



that's a nice idea, just wondering is there anyway i can check dynamically which $_GET variables are currently initialized?.

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.

rocknbil

10:00 pm on Dec 20, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Not sure what you mean, though I wouldn't check get vars in a class or function, that's counter to the whole idea of compartmentalizing the code. What if you want to use this in a post scenario? Yes I know you can use request, but you get the idea . . . or when reading a database, these values may not even be in get or post. It should stand independent.

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.