Run a PHP Pagination with Jquery

Asked

Viewed 265 times

2

I made a script that imports images of a CMS own for Wordpress and not to overload the server and the import machine this system has a pagination that displays 20 elements per page:

http://localhost/system_import/base_importat.php? page=160

I wonder if there would be a way for jquery to run this page and at the end of loading it jquery switch to the next page avoiding that I have to ta clicking on next direct and make the process a little faster:

http://localhost/system_import/base_importat.php? page=161

1 answer

2

You can use the event .scroll() in his window, sort of like this:

$(window).scroll(function() {
   if( $(window).scrollTop() + $(window).height() == $(document).height() ) {
       var url = document.URL;
       var pageNumber = url.split('=')[1];
       var newUrl = "http://localhost/sistema_importa/base_importat.php?pagina=" + pageNumber;

       $(location).attr('href', newUrl);
   }
});

If you want the page to be reloaded when the user gets close to the end, use this:

$(window).scroll(function() {
   if( $(window).scrollTop() + $(window).height() > $(document).height() - 100 ) {
       var url = document.URL;
       var pageNumber = url.split('=')[1] + 1;
       var newUrl = "http://localhost/sistema_importa/base_importat.php?pagina=" + pageNumber;

       $(location).attr('href', newUrl);
   }
});

* Where the 100 condition represents the size in pixels relative to the bottom of the page, you can change to the number you want.

But I do not recommend this solution, I advise you to use some plugin Infinite Scroll/Lazy Loading.

  • Thank you @Eduardo-silva I modified your idea <script>&#xA; $(document).ready(function() {&#xA; //requisição ajax para selecionar postagens&#xA; var url = document.URL;&#xA; var pageNumber = url.split('=')[1];&#xA; var numeroNovo = parseInt(pageNumber)-1;&#xA; var newUrl = "http://localhost/sistema_importa/base_importat2.php?pagina=" + numeroNovo;&#xA;&#xA; $(location).attr('href', newUrl);&#xA; }); // fim document ready&#xA; </script>

Browser other questions tagged

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