GrizRule Posted March 2, 2014 Share Posted March 2, 2014 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 More sharing options...
ignace Posted March 2, 2014 Share Posted March 2, 2014 $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; } Link to comment https://forums.phpfreaks.com/topic/286647-pagination-of-foreach/#findComment-1471208 Share on other sites More sharing options...
GrizRule Posted March 2, 2014 Author Share Posted March 2, 2014 Thanks Ignace. That did the job Link to comment https://forums.phpfreaks.com/topic/286647-pagination-of-foreach/#findComment-1471261 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.