Calculating offset for a for loop - php

I'm trying to do pagination on an array that ive got and currently im looping through it with a for loop like this
for($i = $pages->low;$i<$total;++$i)
What I need to figure out is how to get the $total variable to an be calculated based on the current page and the count of rows so the loop works correctly for the amount of items in the array.
I've got the following variables:
$pages->low (equals the number of rows the pagination has already been through
e.g. Page 1 = 0, Page 2 = 5, Page 3 = 10 etc...
$pages->total_items (explains itself)
$pages->current_page
$pages->ipp (items per page, FYI 5)
So what formula would I use to calculate the amount of rows the loop should go through so for example if there was 13 items in total in the array and 5 results per page, on page one $total should equal 5, page two should equal 10 and page three should equal 13 etc?
Thanks

$total = min($pages->ipp * ($pages->current_page + 1), $pages->total_items);
It does the obivous, but limits it the the total number of items.
Though I personally would simply use a LimitIterator here.

$start_from = ($current_page - 1) * $per_page;
From Kohana's pagination module:
$this->total_pages = (int) ceil($this->total_items / $this->items_per_page);
$this->current_page = (int) min(max(1, $this->current_page), max(1, $this->total_pages));
$this->current_first_item = (int) min((($this->current_page - 1) * $this->items_per_page) + 1, $this->total_items);
$this->current_last_item = (int) min($this->current_first_item + $this->items_per_page - 1, $this->total_items);
$this->previous_page = ($this->current_page > 1) ? $this->current_page - 1 : FALSE;
$this->next_page = ($this->current_page < $this->total_pages) ? $this->current_page + 1 : FALSE;
$this->first_page = ($this->current_page === 1) ? FALSE : 1;
$this->last_page = ($this->current_page >= $this->total_pages) ? FALSE : $this->total_pages;
$this->offset = (int) (($this->current_page - 1) * $this->items_per_page);

not clear, why if there was 13 items on page one total should be equal to 5 ???
For me if you are trying to show the $pages->ipp next items on pages 2 juste go from $pages->low to $pages->low + $pages->ipp

Related

Get numbers next and after a certain number with dynamic max results with PHP

I have a pagination which renders some pages. I have a setting where the user can define how many pages are to be displayed as numbers in the pagination nav. The first and last number are always visible. Now if the user wants to have 3 numbers on the nav and assuming that i am now in the page 7, then it should look like this:
1 ... 6 7 8 ... 12
If the user wants four items then it should look like that:
1 ... 6 7 8 9 ... 12
Up until now i have the following which gives me 3 before and after the current page
$maxLinks = 3;
$currentPageNumber = 7;
$pages = [];
$pages[] = 1;
for($i = max(2, $currentPageNumber - $maxLinks); $i <= min($currentPageNumber + $maxLinks, 12 - 1); $i++) {
$pages[] = $i;
}
$pages[] = 12;
foreach ($pages as $key => $page) {
$newPage = '...';
if (($key === 0) && $pages[1] !== $page + 1) {
array_splice( $pages, 1, 0, $newPage );
}
$itemBeforeLast = count($pages)-2;
if (is_numeric($pages[$itemBeforeLast]) && ($key === $itemBeforeLast) && $pages[$itemBeforeLast + 1] !== $pages[$itemBeforeLast] + 1) {
array_splice( $pages, $itemBeforeLast +1, 0, $newPage );
}
}
This gives me back the following:
But i only want to get 3 or 4 numbers between the dots (this changes based on the value that the user gives in the settings ($maxLinks variable))
Any help is deeply appreciated
Best regards
As you add them to both sides, you need to divide by 2. Also, remove 1 to account for the current page. Then you just need to account for the possibility of having a non-even number of links to the left&right by rounding (down for the left, up for the right).
And end up with:
for($i = max(2, $currentPageNumber - floor(($maxLinks-1)/2));
$i <= min($currentPageNumber + ceil(($maxLinks-1)/2), 12 - 1); $i++)

Modify pagination script to show a different number of rows on the first page than the others

I have a simple pagination script:
$product_nr = count($query); // counts the number of products
$totalpages = ceil($product_nr / $rowsperpage);
if ($currentpage > $totalpages){ $currentpage = $totalpages; }
$offset = (($currentpage - 1) * $rowsperpage);
The code is set to show 20 rows per page -> $rowsperpage = 20;
I need an idea on how to show only 18 rows on the first page ONLY and add the remaining 2 rows to the $product_nr so it won't brake the 20 rows per page pagination for the other pages.
Basically:
1st page - 18 rows;
the other pages would show 20 rows per page from the remaining rows.
Any ideas?
if possible you can set. $rowsperpage separately when on first page ie if $currentpage == 1 then $rowperpage=18
and for all other pages you'll have to set the offset -2
i.e
if($currentpage != 1){ $offset = ((($currentpage - 1) *
$rowsperpage)) - 2 ; }
if($currentpage == 1)
$rowsperpage = 18;
else
$rowsperpage = 20;

Can not send levels to switch pages

Am working on array pagination. I am having some trouble forming equations. what I want is when
$CurrentPage = 1 then $Start = 1,
if $CurrentPage = 2 then $Start = 30,
if $CurrentPage = 3 then $Start = 60,
if $CurrentPage = 4 then$Start = 90
and so on..
How do I write the if else block ?
you can also use this:
$Start = ($currentPage==1 ? 1 : ($currentPage-1)*30);
and forget all the if/else.
If the current page is one, display from result one (for what ever reason not from result 0), if page bigger than one display from result (page - 1)*30
if ((int)$currentPage > 1) {
$start = ($currentPage - 1)*30;
}
else {
$start = 1;
}
or in a shorter way
$start = ($currentPage > 1) ? ((int)$currentPage - 1) * 30 : 1;
Keep it simple:
$Start = max(1, ($currentPage-1) * 30);

php calculation solution for pagination

I have a variable $total which is the total number of results and $page which is the page number. The result is limited to 12 per page.
Suppose if $total is 24, the script may return 1 and 2 for $page=1 and $page=2 respectively. It should also return 1 if the input number is less than 1 (negative or zero) or if the number is greater than 2
Again, suppose if $total is 25, the script may return 1, 2 and 3 for $page=1, $page=2 and $page=3 respectively. It should also return 1 if the input number is less than 1 (negative or zero) or if the number is greater than 1
Here's one way to calculate it:
// Assuming you have the $total variable which contains the total
// number of records
$recordsPerPage = 12;
// Declare a variable which will hold the number of pages required to
// display all the records, when displaying #recordsPerPage records on each page
$maxPages = 1;
if($total > 0)
$maxPages = (($total - 1) / $recordsPerPage) + 1;
// $maxPages now contains the number of pages required. you can do whatever
// it is you need to do with it. It wasn't clear from the question..
return $maxPages;
Further, if you wanted to generate an array containing the indexes of each available page you could just do this:
$pages = array();
for($i = 1; $i <= $maxPages; i++)
{
array_push($pages, $i);
}
print_r($pages);

Calculating number of results in a specific page using pagination

I have a search result from MySQL query or Array or something else. The result gave a variable $totalfiles say for example 25. The number of results is limited to $limit, here 12. The maximum number of pages calculated, $maxpages, will be 3.
As we consider the case, we will get 12 results for pages 1 and 2, and 1 for page 3.
What is the easiest way to predict (or calculate) the number of results in a specific page using variables $totalfiles, $limit, $maxpages and $pagenumber using PHP? I don't think all these 4 variables are necessary for doing this.
The only page that is allowed to have a number that is different from $limit is the last one. Don't forget that. And the last page will always have the remainder of the integer division
$last_page_items = $totafiles % $limit;
If $last_page_items is 0, means that you have all pages with $limit items
also,
$pages = ceil($totalfiles / $limit);
R = Number of rows on a given page, P, where P starts at 1.
T = Total rows
L = Rows per page
R = T - ((P-1) * L)
Then just add a check afterwards to set R to 0 if R < 0.
Code:
function get_num_rows_on_page($page, $total_rows, $per_page) {
$on_page = $total_rows - ( ($page-1) * $per_page );
return $on_page < 0 ? 0 : $on_page;
}
Max pages: Total files, divided by the limit. If the remainder is greater than 0, add one.
$maxpages = $totalfiles/$limit;
if ($totalfiles % $limit > 0) $maxpages++;
Number on current page: if Page number less than Max page, there are limit results. If Page number is the Max page, there are (Remainder of Total Files divided by limit) results if that remainder is greater than 0, otherwise limit.
$results = $limit;
$rem = $totalfiles % limit;
if ($pagenumber == $maxpages && $rem > 0) $results = $rem;
If you want to distribute the results evenly in the maximum number of pages, which is 3 as you have suggested, you can use:
$results_per_page = ($totalfiles/$maxpages);
Otherwise, you already have the number of results per page calculated on your $limit variable.
Try this:
function getPageSize($total, $maxpages, $pagenumber){
$itemsperpage=intval($total/$maxpages);
if($pagenumber == $maxpages){
$itemslastpage=abs($total-($itemsperpage*$maxpages));
return $itemslastpage;
}else{
return $itemsperpage;
}
}
// should print '5'
echo getPageSize(29,6,6) . "\n";
// should print '7'
echo getPageSize(14,2,1) . "\n";
Note that $limit is not needed as it is only used to control results from database.

Categories