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

Asked

Viewed 426 times

2

The variable $_SERVER['REQUEST_URI'] shows this

search.php? page=6&typo_animal=Cat

But what I wanted was to get

search.php? typo_animal=Cat

That would remove the page variable

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

  • I don’t understand why the double vote ...

1 answer

4


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

Do so:

<?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>';
  • But this will bring the link without the page variable ?

  • @Amadeuantunes yes o unset($gets['page']); removes the page= ;)

  • 1

    Solved just fine thank you :$

  • 1

    Perfect worked :D

  • 1

    you can remove " This question already exists and was answered here: How to execute a pagination with PDO?" This question is different

  • 1

    @Amadeuantunes is the cache of the site, it takes a few hours to disappear, but there is no problem to appear linked, one question leads to another, in the other question people will see your or your question is great and will help those who find that other question. It’s like "youtube related videos" only for questions :) - Don’t worry it won’t duplicate more, I’ve already removed my vote and no one else will vote.

  • 2

    Okay thank you ^_^

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.