mało optymalne, ale najłatwiej na to wpaść i najłatwiej zrozumieć działanie:
<?php
$liczby = array(2,3,5,6,8,9,11,16,18);
$ile = 5;
$start = empty($_GET['start']) ? 0 : (int)$_GET['start'];
function xslice($arr, $start, $end){
$wyn = Array();
$c = count($arr);
for($i = $start; $i < $end; $i++)
$wyn[] = $arr[($i % $c + $c) % $c];
return $wyn;
}
$liczby = xslice($liczby, $start, $start + $ile);
?>
<a href="?start=<?= $start-1 ?>"><<</a>
<?= implode(' ', $liczby) ?>
<a href="?start=<?= $start+1 ?>">>></a>
bo trudno jednak z tym slice wymyślić rozwiązanie które się wielokrotnie zapętla (gdy np ile przekracza 3x długość tablicy)
// coś mam dzikie wrażenie że tego typu funkcja jest normalnie w php ...
// kod po rozmowie na gg:
<?php
$liczby = array(2,3,5,6,8,9,11,16,18);
$ile = 5;
$start = empty($_GET['start']) ? 0 : (int)$_GET['start'];
function absEx($n, $c){
return ($n % $c + $c) % $c;
}
function xslice($arr, $start, $end){
$wyn = Array();
$c = count($arr);
for($i = $start; $i <= $end; $i++)
$wyn[] = $arr[absEx($i, $c)];
return $wyn;
}
$prev = absEx($start - 1, $c = count($liczby));
$next = absEx($start + 1, $c);
$start -= floor($ile / 2);
$liczby = xslice($liczby, $start, $start + $ile - 1);
?>
<a href="?start=<?= $prev ?>"><<</a>
<?= implode(' ', $liczby) ?>
<a href="?start=<?= $next ?>">>></a>