GrizRule Posted March 2, 2014 Share Posted March 2, 2014 (edited) 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; } Edited March 2, 2014 by GrizRule Quote Link to comment Share on other sites More sharing options...
Solution ignace Posted March 2, 2014 Solution Share Posted March 2, 2014 (edited) $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; } Edited March 2, 2014 by ignace Quote Link to comment Share on other sites More sharing options...
GrizRule Posted March 2, 2014 Author Share Posted March 2, 2014 Thanks Ignace. That did the job Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.