124697
124697

Reputation: 21893

How can I pass multiple paramters in one

I want to do something like mysite.com/?query="q1=1&q2=2&q3=3"

Ive tried $_GET['query'] but it shows query=q1

Upvotes: 0

Views: 93

Answers (4)

Buksy
Buksy

Reputation: 12218

It would be easier to use another separator, you could use comma for example

test.php?query=q1,q2,q3
$parameters = explode(',', $_GET['query']);

if you need those parameters to have a value, why don't you use json? But you will have to (or at least shoud) urlencode your json object first

test.php?query={"q1":1,"q2":2,"q3":3} // raw json
test.php?query=%7B%22q1%22%3A1%2C%22q2%22%3A2%2C%22q3%22%3A3%7D //encoded json

$parameters = json_decode( urldecode($_GET['query']) );

To encode your parameters, simply use json_encode and urlencode

$array = array( "q1" => 1, "q2" => 2, "q3" => 3 );
$parameters = urlencode( json_encode($array) );

Upvotes: 0

CarlosCarucce
CarlosCarucce

Reputation: 3569

If you need to generate an url with array parameters, you may use http_build_query

Example:

$params = http_build_query(array(
    "query" => array(
        "value1",
        "value2",
        "value3"
    ),
    "another_param" => "hello"
));
echo $params;

The example above will output query%5B0%5D=value1&query%5B1%5D=value2&query%5B2%5D=value3&another_param=hello

(Note that http_build_query does all the encoding job for you)

You will be able to use these parameters after ? in a link:

echo "<a href='http://mywebsite.com/?$params'>link</a>"

Then you can read this parameter from $_GET

if(isset($_GET['query']) && is_array($_GET['query'])){
    foreach($_GET['query'] as $query){
        //Do something with $query value
    }
}

Upvotes: 2

Indrajit
Indrajit

Reputation: 405

Url encode the string you pass inside query parameter like: ?query=%22q1%3D1%26q2%3D2%26q3%3D3%22

$s = $_GET['query'];
parse_str(trim($s,'"'),  $output);
print_r($output);

Output is:

Array ( [q1] => 1 [q2] => 2 [q3] => 3 ) 

Upvotes: 0

Jeff Puckett
Jeff Puckett

Reputation: 40861

You'll need to escape/urlencode the & because it views that as the next query string parameter.

Upvotes: 0

Related Questions