<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
var batch = 0; // Current batch number
// Function to fetch and display the next batch of records
function fetchNextBatch() {
$.getJSON('get_records.php', {batch: batch}, function(data) {
if (data.length > 0) {
// Append the records to your HTML element
// For example, assuming you have a <div> element with id "records"
var recordsDiv = $('#records');
$.each(data, function(index, record) {
recordsDiv.append('<p>Record ' + record.id + ': ' + record.data + '</p>');
});
// Increment the batch number for the next fetch
batch++;
}
});
}
// Fetch the first batch of records
fetchNextBatch();
// Call the fetchNextBatch() function every 10 seconds
setInterval(fetchNextBatch, 10000);
</script>
get_records.php
<?php
// Connect to the database
$mysqli = new mysqli("localhost", "username", "password", "my_database");
// Check connection
if ($mysqli->connect_error) {
die("Connection failed: " . $mysqli->connect_error);
}
// Get the current batch number from the request
$batch = isset($_GET['batch']) ? intval($_GET['batch']) : 0;
// Calculate the offset for the LIMIT clause
$offset = $batch * 10;
// Fetch 10 records from the database
$query = "SELECT * FROM my_table LIMIT $offset, 10";
$result = $mysqli->query($query);
// Fetch the records as an array
$rows = array();
while ($row = $result->fetch_assoc()) {
$rows[] = $row;
}
// Close the database connection
$mysqli->close();
// Return the records as JSON data
header('Content-Type: application/json');
echo json_encode($rows);
?>