seems like paging comes into vogue 😉
here's my attemp, tell me what you think of it
/**
* create page-indices for navigating through results
* @param array $data array containing the complete data
* @param int $per_page number of entries per page (starts with 1)
* @param int $page page to display
* @param int $display_pages number of pages on both sides to display for direct navigation
* @param boolean $preserve_keys
* @access public
* @return array
*/
function paginate(&$data, $per_page = 10, $page = 1, $display_pages = 2, $preserve_keys = true) {
$result = array('back' => false, 'next' => false, 'data' => $data,
'back_page' => NULL, 'next_page' => NULL, 'pages' => array());
//all data fits on one single page
if (($count = count($data)) <= $per_page)
return $result;
$start = ($page - 1 ) * $per_page;
if ($start >= $count)
//start position to big
return $result;
$end = $start + $per_page;
$result['data'] = array();
for ($i = $start; $i < $end; $i++)
if (isset($data[$i]))
$result['data'][$i] = $data[$i];
else break;
if ($page > 1) {
//navigating back is possible
$result['back'] = true;
$result['back_page'] = $page - 1;
}
if ($count > $end) {
//navigating forward is possible
$result['next'] = true;
$result['next_page'] = $page + 1;
}
if (!$preserve_keys)
$result['data'] = array_values($result['data']);
$pages = ceil($count / $per_page);
$display_pages = (int)$display_pages;
$start = max($page - $display_pages, 1);
$end = $start + 2 * $display_pages;
if ($end > $pages) {
$start = max(1, $pages - 2 * $display_pages);
$end = $pages;
}
$result['pages'] = range($start, $end);
return $result;
}
$data = range(1, 500);
$first_10 = paginate($data);
$first_8 = paginate($data, 8);
$second_5 = paginate($data, 5, 2);
$fourth_7_three_pages_before_and_after = paginate($data, 7, 4, 3);
$fifth_4_renumbered = paginate($data, 4, 5, 2, false);
print_r($first_10);
print_r($first_8);
print_r($second_5);
print_r($fourth_7_three_pages_before_and_after);
print_r($fifth_4_renumbered);
And the output:
Array
(
[back] =>
[next] => 1
[data] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 6
[6] => 7
[7] => 8
[8] => 9
[9] => 10
)
[back_page] =>
[next_page] => 2
[pages] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
)
)
Array
(
[back] =>
[next] => 1
[data] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 6
[6] => 7
[7] => 8
)
[back_page] =>
[next_page] => 2
[pages] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
)
)
Array
(
[back] => 1
[next] => 1
[data] => Array
(
[5] => 6
[6] => 7
[7] => 8
[8] => 9
[9] => 10
)
[back_page] => 1
[next_page] => 3
[pages] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
)
)
Array
(
[back] => 1
[next] => 1
[data] => Array
(
[21] => 22
[22] => 23
[23] => 24
[24] => 25
[25] => 26
[26] => 27
[27] => 28
)
[back_page] => 3
[next_page] => 5
[pages] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 6
[6] => 7
)
)
Array
(
[back] => 1
[next] => 1
[data] => Array
(
[0] => 17
[1] => 18
[2] => 19
[3] => 20
)
[back_page] => 4
[next_page] => 6
[pages] => Array
(
[0] => 3
[1] => 4
[2] => 5
[3] => 6
[4] => 7
)
)