Skip to content

Instantly share code, notes, and snippets.

@tajulasri
Last active March 3, 2018 06:17
Show Gist options
  • Save tajulasri/ae05b7c208cf642928fbd93491d45d0f to your computer and use it in GitHub Desktop.
Save tajulasri/ae05b7c208cf642928fbd93491d45d0f to your computer and use it in GitHub Desktop.
workers min max pages problem 1
<?php
/*
This is to solve pagination dividing for each workers for asyncronous feature in API request to third party endpoint with
large response data.
*/
function get_min_max_page($totalPages = 0,$workers = 1) {
$maximumPage = $totalPages / $workers;
$sequenceIncrement = $maximumPage;
$minMaxWorkers = [];
foreach(range(1,$workers) as $worker) {
$startPage = ($sequenceIncrement - $maximumPage + 1);
$minMaxWorkers[] = ['start' => $startPage,'end' => $sequenceIncrement];
$sequenceIncrement += $maximumPage;
}
return $minMaxWorkers;
}
var_dump(get_min_max_page(3000,10));
/*
sample result
array(10) {
[0]=>
array(2) {
["start"]=>
int(1)
["end"]=>
int(300)
}
[1]=>
array(2) {
["start"]=>
int(301)
["end"]=>
int(600)
}
[2]=>
array(2) {
["start"]=>
int(601)
["end"]=>
int(900)
}
[3]=>
array(2) {
["start"]=>
int(901)
["end"]=>
int(1200)
}
[4]=>
array(2) {
["start"]=>
int(1201)
["end"]=>
int(1500)
}
[5]=>
array(2) {
["start"]=>
int(1501)
["end"]=>
int(1800)
}
[6]=>
array(2) {
["start"]=>
int(1801)
["end"]=>
int(2100)
}
[7]=>
array(2) {
["start"]=>
int(2101)
["end"]=>
int(2400)
}
[8]=>
array(2) {
["start"]=>
int(2401)
["end"]=>
int(2700)
}
[9]=>
array(2) {
["start"]=>
int(2701)
["end"]=>
int(3000)
}
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment