Automatic pagination

Hi there!
First I would like to introduce myself shortly. My name is Eric and I am learning php msql for using it at my work. I live in the Netherlands, so I appologize for my bad english, but I’ll keep trying :slight_smile:

Now my question:

I have a mysql database with events. And I want these events to be shown on a public screen, with a maximum of 10 records per page.
I have tried pagination, but the problem with this is that I have to go to the next page manually by clicking a link in the pagination bar.
This is not possible on a public screen. So I want the pages to switch automatically. For example in case of 15 events, the first page shows record 1 to 10, then after (lets say) 10 seconds automatically switch to the second page with record 11 to 15 , end then again after 10 seconds back to the first page.

I hope someone can help me out!..
Thank you!
Regards, Eric

You can do this with a refresh header. For example, the PHP code:

header("Refresh:10");

Will refresh your page after 10 seconds.

You can also use this to refresh to your next screen of events. First, have your events page accept a parameter to show a certain page; for example, mypage.php?page=2 would load the second page.

Now you can do:

header("Refresh:10; url=mypage.php?page=" . $_GET['page'] + 1);

to load the next page. You’ll need some checking to make sure you reset to page 1 after your last page as well.

Personally, I would do this with Javascript/Ajax (I like using Fetch) to control going to the next page as the page will reload dynamically. It really shouldn’t be that hard of script to develop.

…" It really shouldn’t be that hard of script to develop"…
That’s what I thought too, but I’m a beginner :slight_smile:

This is what I have so far…

<?php
$sql = "SELECT COUNT(*) AS FROM tbl_publish";
$result = mysqli_query($conn, $sql);

$r = mysqli_fetch_assoc($result);
     
$numrows = $r[0];
$rowsperpage = 10;
$totalpages = ceil($numrows / $rowsperpage +1);

if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {

$currentpage = (int) $_GET['currentpage'];
} else {

$currentpage = 1;
}

if ($currentpage > $totalpages) {

$currentpage = $totalpages;
} 
if ($currentpage < 1) {

$currentpage = 1;
} 

$offset = ($currentpage - 1) * $rowsperpage;
$sql = "SELECT * FROM tbl-publish ORDER by eventname LIMIT $offset, $rowsperpage";
$result = mysqli_query($conn, $sql);

 
{
echo
"<table id='table'>
<tr> 
<th><div align='left' p style='color:#404040'></div></th>
<th><div align='left' p style='color:#404040'></div></th>
<th><div align='left' p style='color:#404040'></div></th>
<th><div align='left' p style='color:#404040'></div></th>
</tr>";

while($row = mysqli_fetch_assoc($result)) 
	   {
      echo '<tr id="regel">';
	  echo '<td id="logo" style="padding:0px 0px 0px 35px" width="170" style="bgcolor:#D8D8D8;">';
      echo '<img border="0" src='.htmlspecialchars($row["logo"]).' width="129" height="76">';
      echo '</td>';
	  echo '<td id="eventname" style="padding:0px 0px 0px 50px" width="1250" style="color:#8DA54F;">';
	  echo htmlspecialchars($row['eventname']);
      echo '</td>';
      echo '<td id="room" width="450" style="color:#548AB7;">';
      echo htmlspecialchars($row['room']);
      echo '</td>';
      echo '</tr>' . PHP_EOL;                                  
     }
      echo '</table>';
     }
     $range = 1;

for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {

if (($x > 0) && ($x <= $totalpages)) {

} 
} 

$next_page_count = ++$currentpage % $totalpages;
$next_page = $_SERVER['PHP_SELF'] . '?currentpage=' . $next_page_count;
$conn->close();
?>
</div>
<script>
    function switchPage(){
        window.location = "<?php echo $next_page?>"; 
    }

    setTimeout(switchPage,10*1000); 
</script>

problem solved. I had a older script written in mysql. I changed it info mysqli

He Eric, hoe heb je het opgelost? Ik wil precies hetzelfde bereiken. Bedankt alvast!

Hi Eric, how did you get it to work? I want to accomplish the same result. Thanks again.

Can you please share the code?

Sponsor our Newsletter | Privacy Policy | Terms of Service