Array Pagination - Writing Math Equation - php

My question is almost similar to my previous question which is related to Array Pagination.
If $CurrentPage = 1 then I want StartPage = 20, if $CurrentPage = 2 then StartPage = 15, if CurrentPage = 3 then StartPage = 10, if CurrentPage = 4 then StartPage = 5
Note the difference if 5 is due to number of rows per page, which may change. So in case its 10 and if $CurrentPage = 1 then StartPage = 20, if CurrentPage = 2 then StartPage = 10
I just want to know how I go about writing math equation for it
For example, I wrote something like this
$RowsPerPage = 5;
$StartPage = $RowsPerPage * ( RowsPerPage - $CurrentPage);
However, the above works only if RowsPerPage is set to 5.
Any suggestions??

This might be what you want:
$StartPage = 20 - (($CurrentPage - 1) * $RowsPerPage);

$RowsPerPage = 5;
$TotalPages = 20;
$StartPage = $TotalPages - ( ( $CurrentPage - 1 ) * $RowsPerPage );
This should do what you want.

Related

PHP Pagination from Huge JSON array_chunk in to multiple files and getting offset

So I have this huge file that has 45K+ arrays and I can't just open a huge file on live server with high traffic from every request so I used array_chunk($array, 1000) and saved them in 46 files.
Now I want to read those files when specific page is accessed.
Problem?
$offset seems to be working fine with some pages but mostly offset changes to - number (minus number). Checked page 25, 50, 75 and more...
My Math is kinda (Very) weak, so any help will be appreciated. Thanks!
<?php
$page = ! empty( $_GET['page'] ) ? (int) $_GET['page'] : 1;
$limt = 40; //Page array/item limit
$fcnt = 46; //Total number of files
$tarr = 45187; //Total number of arrays
$fmu0 = (int)($limt*$fcnt*$page/$tarr); //Getting the file number according to page number
if(file_exists("$fmu0.json")){
$id = json_decode(file_get_contents("$fmu0.json"),true);
//Each file has 1000 arrays except the last file which has 187 arrays
$tpgs = ceil($tarr/$limt); //Total number of pages
$mult = $fmu0*count($id);
$offset = ($page - 1) * $limt-$mult;
if( $offset < 0 ){$offset = 0;}
$id = array_slice( $id, $offset, $limt );
var_dump($id);
}
?>
1000 objects per file and 40 objects per page makes 25 pages per file.
Here's how to find the file containing objects for $page number:
$fmu0 = floor($page / 25);
And here's how to find the starting index of the group of 40 ($limt) objects within that file corresponding to $page number, when the first page is 1:
$offset = (($page - 1) * $limt) - ($fmu0 * 1000);
<?php
$page = (!empty($_GET['page']) && 0 < $_GET['page'])
? (int)$_GET['page']: 1;
$limt = 40; //Page array/item limit
// FIND FILE, 25 pages per file
$fmu0 = floor($page / 25);
if(file_exists("$fmu0.json")){
$id = json_decode(file_get_contents("$fmu0.json"),true);
// FIND GROUP of 40 ($limt) page objects, 1000 objects per file
$offset = (($page - 1) * $limt) - ($fmu0 * 1000);
$id = array_slice( $id, $offset, $limt );
var_dump($id);
}
?>

Displaying row numbers

So I wanna display the row number (ranking) before every record.
Now the code below shows correct on the first page. 1 thru 25, but then I go the second page then a 6 is added before the row number. Should be 26 thru 50 but instead it's 626 thru 650. Would be correct if the number 6 werent there. And so it goes on, on page 3 its 1251 thru 1275 (should be 51 thru 75). Whats wrong here?
$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
$perPage = 25;
$offset = ($page - 1) * $perPage;
$query "SELECT name, exp FROM people ORDER BY exp DESC LIMIT $offset, $perPage";
<? $i = (($perPage * $offset) +1); foreach($db->query($query) as $row): ?>
Ranking: <?=$i?>
Name: <?=$row['name']?>
<? $i++; endforeach ?>
Your $i calculation is wrong. You have already used $perPage in offset calculation. Why using it again?
$i = (($perPage * $offset) +1);
should be
$i = $offset + 1;
Demonstration for fun

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);

Calculating offset for a for loop

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

Categories