Jump to content

[SOLVED] repost $_GET variables


bruce080

Recommended Posts

I have a search tool that allows filtering by a couple of different categories.  You select a value from a dropdown menu and it will filter the results to only show that specific product or system that was chosen.

 

I also have pagination set up so that it will only display 20 documents per page but it will not repost the variables that were used for the filter.  Is there a simple way to resubmit the current filter variables with another variable for page number?

 

Thanks in advance for your help,

Steven

Link to comment
https://forums.phpfreaks.com/topic/166980-solved-repost-_get-variables/
Share on other sites

Oh great, I think that $_SERVER['QUERY_STRING'] is what I was looking for.  That will make it a lot easier than my nested if statements... haha, I'm worthless.

 

Anyway, I tried this code:

echo "<dd class=\"next\"><a href=\"cc-nse-search.php<?php print $_SERVER['QUERY_STRING'] ? '?' . $_SERVER['QUERY_STRING'] : '';?>\"></dd>";

 

and it gave me this error:

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING

 

Anyone care to enlighten me on what exactly that piece of code is supposed to be doing?  In the meantime I'm going to try and incorporate QUERY_STRING into my code.

 

Thanks for the help guys

Great, I solved the problem.  Here is the code that I am using to create a link to the next page in my search.

 

										
echo "<dd class=\"next\"><a href=\"cc-nse-search.php";
print ($_SERVER['QUERY_STRING'] ? '?' . $_SERVER['QUERY_STRING'] . "&pageno=$next" : '');
echo "\">next</a></dd>";

 

Thanks,

Steven

Wait, no, that didn't quite solve it either.  How do I handle the $pageno variable.

 

If I have:

?category=apples&color=red&pageno=3

 

and I want to make a link to the next page, how do I remove the pageno=3 variable and replace it with pageno=4?

Sorry, I might have phrased that poorly.  I know how to increment the page numbers, but I don't know how to change a variable when creating the link

 

If my current QUERY_STRING is:

?category=apples&color=red&pageno=3

 

my code will just add a variable and create this new QUERY_STRING:

?category=apples&color=red&pageno=3&pageno=4

 

Is it possible to replace or change the original $pageno variable from the string so that I don't keep adding to it?

Sorry, I might have phrased that poorly.  I know how to increment the page numbers, but I don't know how to change a variable when creating the link

 

If my current QUERY_STRING is:

?category=apples&color=red&pageno=3

 

my code will just add a variable and create this new QUERY_STRING:

?category=apples&color=red&pageno=3&pageno=4

 

Is it possible to replace or change the original $pageno variable from the string so that I don't keep adding to it?

 

Yeah, just set $_GET[pageno] = $_GET[pageno] + 1;

You're right, setting $_GET[pageno]=4 will change the pageno variable.  However, this does not solve the problem because $_SERVER['QUERY_STRING'] will still be:

?category=apples&color=red&pageno=3 even if ($_GET[pageno] == 4) now.

 

Instead, I fixed it using substr().  While, admittedly is not the best solution, it will work for now.  Here is my code:

$qString = $_SERVER['QUERY_STRING'];
$newqString = substr($qString, 0, strrpos($qString, '=') +1);
echo "<dd class=\"next\"><a href=\"cc-nse-search.php";
print ($qString ? '?' . $newqString . "$next" : "?pageno=$next");
echo "\"> </a></dd>";

 

This code will remove anything past the last '=' sign and will replace it with the new number.  This currently works because pageno is always the last variable in the URL, however, its probably not a permanent solution.

 

If someone comes up with a better idea, can you post please?

 

Thanks again guys,

Steven

If someone comes up with a better idea, can you post please?

 

function createQueryString($array = array(), $kvSep = '=', $varSep = '&') {
    $get = array_merge($_GET, $array);
    $getVars = array();
    foreach ($get as $key => $value) {
        if (is_string($key)) {
             $getVars[] = implode($kvSep, array($key, $value));
        }
    }
    return implode($varSep, $getVars);
}

 

<a href="page.php?<?php print createQueryString(); ?>">

<?php $_GET['pageno']++; ?>

<a href="page.php?<?php print createQueryString(); ?>">

<a href="page.php?<?php print createQueryString(array('pageno' => 5)); ?>">

 

Outputs:

<a href="page.php?category=apples&color=red&pageno=3">

<a href="page.php?category=apples&color=red&pageno=4">

<a href="page.php?category=apples&color=red&pageno=5">

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.