0

For example I have a URL like this one:

index.php?country=Canada

If it is just index.php that means that the default country is USA. People can switch between countries by checking or unchecking a checkbox. But people can sort their results via GET variables:

<a href="<?php $_SERVER["REQUEST_URI"] ?>&sort=state">State</a>
<a href="<?php $_SERVER["REQUEST_URI"] ?>&sort=surname">Surname</a>
<a href="<?php $_SERVER["REQUEST_URI"] ?>&sort=name">Name</a>

But if I use $_SERVER["REQUEST_URI"] then it will always just keep adding new values to my URL array (query string). It works if it is just index.php then I can make it like this:

<a href="<?php $_SERVER["PHP_SELF"] ?>?sort=state">State</a>
<a href="<?php $_SERVER["PHP_SELF"] ?>?sort=surname">Surname</a>
<a href="<?php $_SERVER["PHP_SELF"] ?>?sort=name">Name</a>

I know that after index.php it will always be a question mark ? first. But what when visitors want to keep index.php?country=Canada and just switch between sort=state, sort=surname and sort=name. Then I need to know if a question mark is already in the URL, when to add & mark. I'm not sure how to solve this problem.

4 Answers 4

2

Change the way you echo your link:

PHP

<?php
    $link = $_SERVER["PHP_SELF"];
    if(isset($_GET['country']
        $link.="&";
    else
        $link.="?";
?>

And echo link like this:

HTML

<a href="<?php $link ?>sort=state">State</a>

NOTE: I deleted the question mark before "sort".

Sign up to request clarification or add additional context in comments.

Comments

1

You need logic to dynamically build a querystring, rather than statically adding ? and &.

Something like http_build_query would be the route I'd go, but the information you've provided is small compared with the possibilities you appear to want, so its hard to provide specific code.

Here is some more information about the http_build_query function in PHP. Its purpose is to build a querystring, here is an example of what you might do:

// capture the values into variables, however you want ($_GET is example)
$country = $_GET['country'];
$state = $_GET['state'];
$city = $_GET['city'];
$data = array();

// use logic to dynamically build the array, so long as the variables have values
!empty($country) ? array_push($data,'country'=>$country) : '';
!empty($state) ? array_push($data,'state'=>$state) : '';
!empty($city) ? array_push($data,'city'=>$city) : '';

// apply the array dynamically built
$querystring = http_build_query($data);

// concatenate to form the new URL
$url = 'http://www.example.com/?'.$querystring

If you declared country to be USA and city to be Seattle, but did not declare state, it would produce:

http://www.example.com/?country=USA&city=Seattle

Something along these lines should build the dynamic array you want with only the values you are looking for.

1 Comment

Thanks all for your help, I will use all that to figure out solution to my problem
1

All of the variables in the query string are stored in the $_GET php superglobal array. In your example you can access the country with $_GET['country']. Do default to the USA you can use isset() to check if the variable exists.

$country = "USA";
if(isset($_GET['country'])) {
   $country = $_GET['country'];
}

Comments

1

The solution here is to use either $_GET (already mentioned in another answer), or http://php.net/manual/en/function.parse-url.php, which makes the ordering of arguments completely irrelevant. Simply parse the url into an array of query arguments, and test for the ones you need. To turn it all back into a url, you would use http://php.net/manual/en/function.http-build-query.php

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.