How to switch between URL variable names and values - php

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:
State
Surname
Name
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:
State
Surname
Name
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.

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
State
NOTE: I deleted the question mark before "sort".

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.

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'];
}

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

Related

How i can writte this in php for strings and no give me error

$array_names=array("phone","car","house");
I have this string: $phone_[$jr]
But the string with string called phone it's a name for value, i want use other names for get values, by this it´s necessary write using as this :
${$phone}
The problem it´s i need use other thing more in this string :
${$phone}_[$jr]
And this give me error
I see the problem it´s with "_" but i need use, my question it´s about how i need writte right for works
${$phone}_[$jr]
In other cases i need use :
${$house}_[$jr]
Etc, .....
Thank´s
I assume that you have a scenario like this:
// this is the array you are trying to access dynamically
$bar_ = [];
// this will hold the first part of the variable name
$phone = 'bar';
// and this is how you would build the string which is the full name of the array
// You can then use it to access one of its elements.
${$phone.'_'}[1] = 'f';

GET multiple URL variables in PHP

I have this piece of code:
$user = $_GET["user"];
echo $user;
which outputs:
1234
And the URL looks like this :
http://localhost/getkey.php?user=1234
How can I get multiple variables from the URL separated by a "&"?
For example:
http://localhost/getkey.php?user=1234&password=4321
To get the values passed in the URL you need to call $_GET['name'] for each value that you want.
Example:
http://localhost/getkey.php?user=1234&password=4321
$user = $_GET['user'];
$password = $_GET['password'];
This way will allow to you to use the value passed in the URL
extract($_GET);
This assigns the values in $_GET to variables with the same names as the keys. Don't do this though - it's much better to be explicit and assign each one of your variables separately so that you can trace what is used where better later and so that you don't run into naming conflicts. For this reason, it is also insecure, because your variables can be overridden by user-defined ones, which could be malicious.
See discussion here: What is so wrong with extract()?

how to read ending URL numbers as a value?

I have a survey page that will reference an invoice number. I'm trying to make the URL clean so that there are no question marks or equal sign. For example:
http://www.MyWebsite.com/survey/832551
Where 832551 is the invoice ID in reference. I am trying to avoid ?invoice_ID=832551.
How can I get a php page to read that and make it a variable value?
php 4.0.8
Make an variable with url
//You get all the url in array
$var = $_SERVER["REQUEST_URI"];
//Explode it in array, with slash '/'
$var_array = explode("/",$_SERVER["REQUEST_URI"]);
//Get the last element of array, which will be the invoice_id
$invoice_id = array_pop($var_array);
But if you use some of php framework this is how you get controller attribute by default,
I suggest you to use some php framework with MVC structure.
It is what we call URL rewriting.
You can find a good example here
http://www.addedbytes.com/articles/for-beginners/url-rewriting-for-beginners/

Pass PHP variable to server

So I have this variable of $date = array($year,$month); inside a couple of nested foreach statements. I have a link that when pressed should pass the $date variable over to my functions.php for me to play around with.
I'm using wordpress and so far I understand the link has to work something like this:
$link = admin_url('admin-ajax.php?[$date variable needs to go here]&post_id='.$post->ID.'&nonce='.$nonce);
Basically my question is how exactly does the link above need to be formatted to send my variable? Also, on the server side, how best to receive that variable?
First of all, you can't send arrays directly through GET requests (GET requests are the ones with the parameters visible in the url, in layman terms)
therefore, you should do the following:
$date = "$year-$month"; //example: 2013-09
$link = admin_url('admin-ajax.php?my_date='.$date.'&post_id='.$post->ID.'&nonce='.$nonce);
breaking the url down to the components, in layman terms:
everything before the ? is the server address and the page that needs to be served
everything after is the so-called "querystring", which is a sequence of pairs (A=B) separated by ampersands &.
so, a URL that looks like this
www.example.com/dynamic_page.php?A=B&C=D&E=F
means:
visit www.example.com, fetch the page named "dynamic_page.php" and use the value B for the variable A, the value D for the variable C and the value F for the variable E.

PHP Get Values out of URL Query String without Key / Value pairs

I need to get the values out of a URL query string like this:
http://exmaple.com/?xyz.123
And assign them to variables inside index.php running on example.com, so that:
$name = xyz;
$number = 123;
How do I code this?
Thanks!!
list($name,$number) = explode('.',$_SERVER['QUERY_STRING']);
You can parse it with the following, though it is ripe for injection unless you perform some validation/sanitization afterwards:
list($name, $number) = explode('.', $_SERVER['QUERY_STRING']);
What you want to do is take a look at $_SERVER['QUERY_STRING']. Explode the query string by . to get an array of values. You can then set up the appropriate variables. Keep in mind you'll also probably want to do some validation on the data to ensure it's in the format you need.
You'd need to setup a mod_rewrite rule first like this (untested)...
RewriteRule ^([a-zA-Z]+)\.([0-9]+)$ index.php?name=$1&number=$2
Then you could pull them out from $_GET in PHP.

Categories