$_GET contents

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

$_GET contents

Post 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. :)
User avatar
R4000
Forum Contributor
Posts: 168
Joined: Wed Mar 08, 2006 12:50 pm
Location: Cambridge, United Kingdom

Post 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.
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

*cough* $_SERVER['QUERY_STRING']
User avatar
R4000
Forum Contributor
Posts: 168
Joined: Wed Mar 08, 2006 12:50 pm
Location: Cambridge, United Kingdom

Post by R4000 »

true you could use that :)
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Post 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. :)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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 @.
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Post 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.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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;
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Post Reply