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
Share on other sites


Just make sure the variables are on each page.

 

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

 

Blue being what they selected (SELECT * FROM harvest WHERE category='$_GET[category]' AND color='$_GET' LIMIT $x,$y). $x and $y being the limits determined by $_GET


.
Link to comment
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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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;

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.