automatic pagination cycle of TABLE with PHP and jquery

2

I have a table already paged, and I need a cycle that is showing the records every certain time so the setTimeout in my case the variable $ b is responsible for creating the buttons that contain the href depending on the records more buttons are being created, by pressing one of these the value is passed to the variable "page".

This is the code that creates the buttons

for($b=1; $b<=$a; $b++)
{
    ?><a href="paging.php?page=<?php echo $b;?>">
<?php echo $b." ";?> </a> <?php
}

This is my javascript code

setTimeout(function(){
<?php
$b=1;
?>

   location.href="paging.php?page=<?php echo $b?>"

 <?php $B++?>
    } , 2000); 
</script>

I hope someone can help me.

    
asked by Andrés Hernandez 23.05.2016 в 05:53
source

1 answer

0

It seems that there is some confusion between what PHP is and what JavaScript is in the code. You should separate them better and keep in mind that PHP values will not be available in JS (at least not after the page loads).

Apart from that, you must be careful because both PHP and JavaScript are case sensitive, that is, they are case-sensitive ($ b and $ B are not the same).

The PHP code that generates the links is fine. All you have to do is change the JavaScript code so that the page is updated every 2 seconds. For this you only require few changes:

  • Check that the page is correct
  • Calculate the next value (in PHP)
  • Passes that value to JavaScript (when the page is created)

The code could be something like this:

<?php
   // si hay un parámetro page que es numérico y page+1 sigue siendo una página válida
   if (isset($_GET["page"]) && is_numeric($_GET["page"]) && $_GET["page"]+1 <= $a) {
       // entonces, la próxima página será page+1
       $proxima = $_GET["page"] + 1;
   } else {
       // si no, la siguiente página será la primera
       $proxima = 1;
   }
?>
<script>
setTimeout(function(){
   var b = <?php echo $proxima; ?>;
   location.href = "paging.php?page=" + b;
} , 2000); 
</script>
                                    
answered by 23.05.2016 / 06:23
source