I am currently looking for a way to split a database to send messages after 60 minutes or so. I have a table with over 1000 rows being returned from the database and would like to split them into groups of 50. I would appreciate an example of how this can be attained. Thank you.
You could create a script that is navigated to like the following:
runner.php?start=0&limit=50
This page would start at row 0 and do 50 repeats. Then after 50 repeats it would echo out:
<script>setTimeout(function() {
window.location='runner.php?start=50&limit=50';
}, 3600000)</script>
Where start=50
would be the $_GET['start'] + $_GET['limit']
You would need to keep your browser window opened though.
You can use array_chunk
:
$chunks = array_chunk($results, 50);
foreach ($chunks as $i => $chunk) {
if ($i != 0) {
sleep(3600); // Pause an hour except at the beginning
}
foreach ($chunk as $row) {
// Do what you want with the row
}
}
Don't do this on a web page, as the browser will time out waiting for the results. But you can do it in a CLI script.