Pass parameters via GET and include more parameters

2

I have a query page, where you can include several parameters in this query. I would like to concatenate more parameters if parameters already exist in that query. In short, take the parameters that already exist and concatenate if the user chooses any more.

Ex: <a href="?sexo=m">Masculino</a> <a href="?cor=verde">Verde</a>

    
asked by anonymous 03.07.2015 / 19:50

2 answers

2

If the query came by GET in an address similar to this:

http://site/page.php?b=1&c=2

And you want the <a href="?cor=verde">Verde</a> link to get b=1&c=2 , you can use $_SERVER['QUERY_STRING'] .

For example:

<?php
//cria a variavel vazia
$qs = '';

//Verifica se QueryString é vazio, se não for então seta a variavel $qs
if (false === empty($_SERVER['QUERY_STRING'])) {
   //O & no final é para separar as variaveis
   $qs = $_SERVER['QUERY_STRING'] . '&';
}
?>

<a href="?<?php echo $qs; ?>cor=verde">Verde</a>

To avoid repeated values, you can use an array using parse_str and http_build_query :

<?php
$qs = '';

if (false === empty($_SERVER['QUERY_STRING'])) {
   $qs = $_SERVER['QUERY_STRING'] . '&';
}

//extrai a querystring para uma array/vetor
parse_str($qs, $output);

//Exibe o resultado
print_r($output);
?>

<?php
//Copia o output para não misturar os links
$link1 = $output;

//Cria o seu link com o valor cor=verde
$link1['cor'] = 'verde';
?>
<a href="?<?php echo http_build_query($link1); ?>"></a>

<?php
//Copia o output para não misturar os links
$link2 = $output;

//Cria o seu link com o valor sexo=m
$link2['sexo'] = 'm';
?>
<a href="?<?php echo http_build_query($link2); ?>"></a>

This will prevent something like:

?cor=verde&cor=verde

Then first the user chooses cor=verde and the url looks like this:

./page.php?cor=verde

Then he chooses sexo=m and the url looks like this:

./page.php?cor=verde&sexo=m
    
03.07.2015 / 20:36
0

Use & at the beginning of a new parameter.

   ....
    <a href="?sexo=m&cor=verde">Masculino e Verde</a>
    ....
    
03.07.2015 / 20:34