I have a array with 2500 elements..I want simple algorithm to get the following.
If the input is 1 , then it should return 1st 50 elements. If the input is 2 it should return 51 to 100th element. if input is 3 it should return 101 to 150th element.
I am too much confused with this algorithm and my mind is not giving any idea,Some one please help me.
Note: i am coding my own pagination for 2500 url..And i need just algorithm idea..not coding
<?php
$ar = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30];
function return_frame($arr, $multiplier, $frame_size=50, $preserve_keys = false) {
return array_slice($arr, ($multiplier - 1)*$frame_size, $frame_size, $preserve_keys);
}
print_r(return_frame($ar, 2, 15, false));
?>
Array
(
[0] => 16
[1] => 17
[2] => 18
[3] => 19
[4] => 20
[5] => 21
[6] => 22
[7] => 23
[8] => 24
[9] => 25
[10] => 26
[11] => 27
[12] => 28
[13] => 29
[14] => 30
)
Take a look at: http://php.net/manual/en/function.array-slice.php
i think you just can add position and range on your sql code like this :
"select * from blabla order by post desc limit $position,$range"
You have indexes from 0 to 2499 right.
you want a $count of 50. Let me show you the thinking process.
so if you get 1 as a $paramter
you want $start
to be 0 and $end
to be 49.
lets see:
1-$parameter
is $start right. and 50-$paramter
is $end . ok
for 2 you want $start to be 50 and $end to be 99;
the above does not work here. lets play with the $count
$parameter*count;
is almost $end.. we have to play for the 0 indexing. so:
$end
will be $parameter*$count-1
. we can see that this is true for 1,2 and also 3 as $parameters
. $start
will be ($parameter-1)*$count
. just quickly in the head that gives 0,50,100. just what we want.
as @cars10 suggested we do not need the $end
. with array_slice($arr,($parameter-1)*$count,$count);