How to remove a variable from $ _SERVER ['REQUEST_URI']?

2

The variable $_SERVER['REQUEST_URI'] shows this

  

search.php? page = 6 & animal_type = Cat

But what I intended was to get

  

search.php? animal_type = Cat

What would be to remove the variable page

What I need is to bring the whole link without the variable page= to be able to add it to the page that is in a loop.

    
asked by anonymous 24.02.2016 / 18:50

1 answer

4

The $_SERVER['REQUEST_URI'] brings the entire url, including the query string, if it concatenates it will duplicate the pag= and there will be cases that the url will be .php&pag= .

Do this:

<?php
    //Pega a URL da página atual .php
    $fullUrl = $_SERVER['PHP_SELF'];

    //Se não tiver _GET então usa o ?
    $signConcat = '?';

    //Verifica se tem variaveis _GET
    if (empty($_GET) === false) {
         //Copia variáveis
         $gets = $_GET;

         //Verifica se existe 'page' e remove ela
         if (isset($gets['page'])) {
              unset($gets['page']);
         }

         //Se tiver qualquer _GET adiciona o & no prefixo
         $signConcat = '&amp;';

         //Adiciona ao $fullUrl o $gets "formatado"
         $fullUrl .= '?' . http_build_query($gets, '', '&amp;');
    }
?>

And call in your loop like this:

<a href="' . $fullUrl . $signConcat . 'page=' .$i. '">'.$i. '<span class="sr-only">(current)</span></a></li>';
    
24.02.2016 / 19:12