Page 1 of 1
$_GET contents
Posted: Fri Mar 10, 2006 2:51 pm
by evilmonkey
Hello,
I am making a search page based on $_GET. The page needs to be paginated, so values of $_GET['page'] will need to get updated as the user clicks the next/prev buttons. The problem is, everything is $_GET will dissappear if I do that. How can I go around this problem? Thanks.

Posted: Fri Mar 10, 2006 2:59 pm
by R4000
Easy, use sessions.
Or if you really can't be bothered.
Use these two functions:
Code: Select all
function create_next($link_to_next,$arguments){
echo "<form action=\"".$link_to_next."\" method=\"get\">";
foreach($arguments as $key=>$val){
echo "<input type=\"hidden\" name=\"".$key."\" value=\"".$val."\">";
}
foreach($_GET as $key=>$val){
echo "<input type=\"hidden\" name=\"".$key."\" value=\"".$val."\">";
}
echo "<input type=\"submit\" value=\"Next\">";
echo "</form>";
}
function create_prev($link_to_prev,$arguments){
echo "<form action=\"".$link_to_prev."\" method=\"get\">";
foreach($arguments as $key=>$val){
echo "<input type=\"hidden\" name=\"".$key."\" value=\"".$val."\">";
}
foreach($_GET as $key=>$val){
echo "<input type=\"hidden\" name=\"".$key."\" value=\"".$val."\">";
}
echo "<input type=\"submit\" value=\"Previous\">";
echo "</form>";
}
Basicly call them like:
Code: Select all
$next = array("test" => "lol");
create_next("search.php",$next);
In my system, this would echo: (if i had ?start_id=10)
Code: Select all
<form action="search.php" method="get">
<input type="hidden" name="test" value="lol">
<input type="hidden" name="start_id" value="10">
<input type="submit" value="Next">
</form>
But i do recommend sessions.
Posted: Fri Mar 10, 2006 3:08 pm
by evilmonkey
How would I user sessions? Basically, the question is, how can I dump the $_GET as a stirng into a variable? If I can get contents of $_GET into a variable, I can just cancatanate the value of $page to the end of that string.
Posted: Fri Mar 10, 2006 3:12 pm
by feyd
*cough* $_SERVER['QUERY_STRING']
Posted: Fri Mar 10, 2006 4:24 pm
by R4000
true you could use that

Posted: Mon Mar 20, 2006 10:38 am
by evilmonkey
Sorry, been away from this project a few days. Anyway, inside the query string, there is a page number. I need to incriment that page number for my "next" button (pagination). Anyway, I'm trying to do it using preg_replace, but regex and I just don't play well together. Here's my code:
Code: Select all
$prev_page = preg_replace("\page=(0-9)","page=".$page-1, $_SERVER['QUERY_STRING']);
I then want to pass the new "querystring" to smarty to serve as the link. This is the warning PHP spits out:
Code: Select all
Warning: preg_replace(): Delimiter must not be alphanumeric or backslash in /home/public_html/search.php on line 115
Little help?
Thanks.

Posted: Mon Mar 20, 2006 10:54 am
by feyd
It means exactly as it says, you didn't set your delimiter for the pattern correctly. I believe your pattern isn't the logic you want either.
PCRE calls require delimiters for the pattern, this delimiter is a symbol character, any, technically, but it is recommended to not use one that is used in patterns. Typical delimiters are /, #, and @.
Posted: Mon Mar 20, 2006 2:27 pm
by evilmonkey
Bah! To the bowels of hell with this bloody regex! I solved the problem using str_replace(), since i realized that i'm always matching the same thing, and I have the value of $page anyway. Regex will only slow down my script and make my life difficult. Thanks guys.
Posted: Mon Mar 20, 2006 4:11 pm
by s.dot
hmm
Code: Select all
$str = $_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING'];
$stripped = preg_replace("#page=\d{1,2}#i",$str);
echo $stripped;