Php post to self including query parameter

I am trying to make a page with pagination of returned sql data.
But I need to keep the query parameter to get the search value.
for example. right now my page is site.com?s=term
when you click next 10 results, the code is using "<a href=" .$_SERVER['REQUEST_URI']."?page=$page\">Next 10 Records</a>"
 I am running into a problem that there are now 2 question marks on my query parameter.
how do I have to set it up so the page with recognize, if there is a ? or not, to put it in, since not always will there be a query parameter.
rivkamakAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
a possible quick fix could be like this:

...
<a href=" .str_replace("page=".$_GET['page'],"page=$page",$_SERVER['REQUEST_URI'])."\">Next 10
...
Dave BaldwinFixer of ProblemsCommented:
Try "<a href=\"".$_SERVER['PHP_SELF']."?page=$page\">Next 10 Records</a>"

http://php.net/manual/en/reserved.variables.server.php
Julian HansenCommented:
Ray PaseurCommented:
The canonical article on pagination with PHP is by Lyndon Baptiste.  Old, but it has withstood the test of time:
http://www.sitepoint.com/perfect-php-pagination/

REQUEST_URI contains all of the request arguments in addition to PHP_SELF.

Do not use unfiltered external inputs, since this may create a security exposure.  The variables in $_SERVER are external inputs, so you want to filter them!  Our examples here are mostly intended to show you where the information might be found in a perfect world.  Unless you live in that world, test for valid and reasonable values. :-)
Ray PaseurCommented:
When I first read this question I thought it was about pagination, but on re-reading, it may be about how to interpret and manipulate the request variables in the URL.  PHP has built-in functions for that.  Let's see if this helps...  The strategy here is to keep all the existing request variables, if any, and add a page= argument to the URL.
http://iconoun.com/demo/temp_rivkamak.php
http://iconoun.com/demo/temp_rivkamak.php?s=term
http://iconoun.com/demo/temp_rivkamak.php?s=term&q=foo
<?php // demo/temp_rivkamak.php

/**
 * http://www.experts-exchange.com/questions/28691277/Php-post-to-self-including-query-parameter.html
 */
error_reporting(E_ALL);

// GET THE URL PATH
$url = parse_url($_SERVER['PHP_SELF']);

// GET THE EXISTING REQUEST ARGUMENTS
parse_str($_SERVER['QUERY_STRING'], $arg);

// INJECT A "page" ARGUMENT
$arg['page'] = 3;

// REBUILD THE URL PATH WITH THE NEW + EXISTING ARGUMENTS
$qry = http_build_query($arg);
$new_url = $url['path'] . '?' . $qry;

// SHOW THE WORK PRODUCT
echo PHP_EOL . "THE NEW URL IS $new_url";

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.