Skip to content
Advertisement

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!

JavaScript

Advertisement

Answer

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:

JavaScript

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:

JavaScript

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement