Jump to content

Pagination of foreach();


GrizRule

Recommended Posts

I have a blog with the blog entries in a directory (very unsecure, I know) and on my home page I have a foreach(); function that retrieves all of the files (blog entries) from the entries directory. I've posted so many entries, my homepage is taking a very long time to load. Is there any way to paginate this foreach(); function?

 

Here is my code

foreach (glob("entries/*.php") as $filename)
{
    include $filename;
}
Link to comment
https://forums.phpfreaks.com/topic/286647-pagination-of-foreach/
Share on other sites


$current_page = isset($_GET['page']) ? intval($_GET['page']) : 1;
$items_per_page = 20;
$offset = ($current_page - 1) * $items_per_page;
$items = glob("entries/*.php");
$total_items = count($items);
$total_pages = ceil($total_items / $items_per_page);

foreach (array_slice($items, $offset, $items_per_page) as $entry) {
include $entry;
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.