If I have well understood your class, the getPages() method returns an array with the pages to display.
In this case, the array must return
First .. 4 5 6 7 8 .. Last
So, must return this array (if we have 10 pages) : 1|..|4|5|6|7|8|..|10
the if statement verfies that you store the good pages. Indeed, if you have 10 pages and if the current page is the 6th, you must store:
* the first one: if(x==1
* the last one: $x == $this->totalPages
* and both which are around the current page: abs($x-$this->currentPage) <= $this->currentSpan
Let's see more clearly the third statement:
if currentSpan = 2: the absolute value of the difference between the current page and the value of the loop must be inferior or equal to 2
this is the detail of your
while($x <= $this->totalPages) loop:
$x = 1: respects the first statement (x=1) -> we take it !
$x = 2: do not respects any statement -> do not take it
BUT
respects the if(x==2) so we store a ".."
$x = 3: do not respects any statement -> do not take it
$x = 4: respects the third statement (the absolute value of 4-6 is 2 -> the current span) -> we take it
$x = 5: respects the third statement (the absolute value of 5-6 is 1 -> inferior to the current span) -> we take it
$x = 6: respects the third statement (the absolute value of 6-6 is 0 -> inferior to the current span) -> we take it
$x = 7: respects the third statement (the absolute value of 7-6 is 1 -> inferior to the current span) -> we take it
$x = 8: respects the third statement (the absolute value of 8-6 is 2 -> the current span) -> we take it
$x = 9: do not respect any statement -> do not take it
BUT
respects the if(x == $this->totalPages-1) -> we take it
$x = 10: respect the second statement ($x == $this->totalPages) -> we take it
I've made some errors in the previous version. It is more that:
PHP Code:
public function getPages($results) {
$this->totalResults = count($results);
$totalPages = $this->totalResults / $this->totalPerPage;
$this->totalPages = ceil($totalPages);
$x = 1;
$array = array();
while($x <= $this->totalPages) {
if($x == 1 || $x == $this->totalPages || abs($x-$this->currentPage) <= $this->currentSpan) {
$array[] = $x;
}
else if($x == 2 || $x == ($this->totalPages - 1)) // take care here. If $totalPages is too small, it might cause some errors in display.
$array[] = "..";
}
return $array;
}
It's more clean ?