GET error in PHP paging

Asked

Viewed 286 times

0

I am trying to adapt the relative links to absolutes in my pagination script. However, whenever I try to go forward or back a page, the link is as follows:
http://localhost/textos?pag=2?pag=3

Script:

<?php
$url          = $_SERVER['SERVER_NAME'];
$urlEndereco  = $_SERVER ['REQUEST_URI'];
?>
<table border="1">
   <tr>
<?php
if($pag!=1){
   echo "<td><a href='http://".$url.$urlEndereco."?pag=".($pag-1)."'> Página Anterior</a></td>"; 
}
if($contador<=$maximo){
   echo "<td>Existe apenas uma única página</td>";
}
else{
   for($i=1;$i<=$paginas;$i++){
if($pag==$i){
   echo "<td  style='background: red'><a href='http://".$url.$urlEndereco."?pag=".$i."'> ".$i."</a></td>";
}else{
   echo "<td><a href='http://".$url.$urlEndereco."?pag=".$i."'> ".$i."</a></td>";
}
}
}
if($pag!=$paginas){
   echo "<td><a href='http://".$url.$urlEndereco."?pag=".($pag+1)."'> Próxima Página</a></td>";
}
?>
</tr>
</table>

However, if I use the code below, it works normally. But it turns out that I will use paging with a include on several pages to clean the code, having an easier control.

echo "<td><a href='textos?pag=".($pag+1)."'> Próxima Página</a></td>";

I also realized that the $urlEndereco that’s causing this, since it does the $_SERVER['REQUEST_URI'];, always return the end of the url: http://localhost/textos?pag=2?pag=3

  • 1
  • Not necessarily, his problem is as a URL, which has nothing to do directly with the subject matter dealt with in this other pagination post.

  • Yes, but anyway it was of great help. Thank you!

2 answers

1


There are several methods to solve.

Cause:

The $urlEndereco is inserting the ?pag=2, when trying to add the new ?pag= he’s doubling.

Resolve:

Remove the 2 of ?pag=2 and just insert the new number in its place.

Use the following to remove the last number:

    <?php
    if(isset($_GET['pag'])){ 
   // Se existir pag ele corta!

      $tamanhoGET = 0 - strlen($_GET['pag']); 
    // Terá o tamanho do GET, ou seja, 1 caractere ou 2...
    // O número zero é para torna-lo negativo!

      $urlEndereco = substr($_SERVER ['REQUEST_URI'], 0, $tamanhoGET);
    // Logo o http://localhost/textos?pag=2 irá se tornar http://localhost/textos?pag=
    // Se tiver na página 12 irá cortar os 2 ultimos números.

    }else{
      $urlEndereco = $_SERVER ['REQUEST_URI'].'?pag=';
   // Este é o caminho padrão

    }  


    ?>

Now just change your echo to just insert the number instead of the whole parameter.

Use something similar:

<?php
     echo "<td><a href='http://".$url.$urlEndereco.($pag+1)."'> Próxima Página</a></td>";
    // O $urlEndereço não terá o número, agora irá possuir o $pag+1. 
?>

General Idea:

I believe it is self-explanatory, but in general it will remove the parameter number and insert a new number, instead of adding a new parameter as it was doing, which would eventually duplicate it.

  • Hello friend! After using your method, you returned two errors: the link has now become http://localhost2/ when I step to the next page. And when I access localhost/texts, the following message is displayed: Undefined index: pag in C: wamp www scripts paginacao.php on line 3, and line 3 is the variable $sizeGET

  • 2

    I made the necessary changes

  • Thank you and congratulations. I made the changes and it worked perfectly. I want to ask another simple question, even though it doesn’t have much to do with the question: Does Google load the tests? for example, in the results will appear texts? pag=1 ? if yes, how do I remove? so that only www.site.com.br/texts appear. As for the friendly url, I will make the changes to get texts/page/1 or page-1, if you know how to help me too.

  • This should be modified in htaccess to a user-friendly url. In the case of Google it does not use a page, but rather the offset value. There is start= in the URL. Thus, the second page is start=10, in the third grade start=20. That is, if you put google.com/search?q=busca&start=50 will fall on page 6.

  • I may not have understood it very well. If you refer to SEO, it will track, but it can also make a sitemap dynamic, pointing each page. But be careful not to have duplicates, especially if it is ? pag=0, for example be repeated without ?pag=0, for example, ideally use the Location header in these cases.

1

Try this way because PHP_SELF is relative to the document root. Delete the $urlEndereco

$url = $_SERVER['PHP_SELF'];

  • The method you gave me is valid. However, it looks for the relative address, and this causes it to take the subfolders of my site and also the extension. php, causing problems with my friendly url’s.

  • Then try with HTTP_REFERER.

  • I don’t know why, but it caused slowness in each request with F5 on the page..

  • You can still use SCRIPT_NAME which returns only the file name, not having the need to load the HOST.

Browser other questions tagged

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