Jump to content

Only loading files on specific pages


GrizRule

Recommended Posts

I have my website paginating files that are shown onscreen. How can I make it so the page only loads the first page's files and then when going on to the next page, it loads the files on that page? Basically what I am trying to do is reduce load time by not loading the files on the pages after page one until the user actually goes to a page after page one.

 

index.php

<?php 

$current_page   = isset($_GET['page']) ? intval($_GET['page']) : 1;
$items_per_page = 10;
$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;
}

echo "<table summary=\"\" cellpadding=\"10\" cellspacing=\"0\"  border=\"0\" class=\"global-links-menu\"><tr>";

if($current_page != 1)
{
    $back_page = $current_page - 1;
    echo "<td ><p><a href='?page=$back_page'>Back</a></p></td>";
}
else
{
    $back_page = $current_page - 1;
    echo "<td ><p></p></td>";
}

for($j=1;$j<=$total_pages;$j++)
{
    
    if($j==$current_page)
    {        
        echo "<td ><p>$current_page</p></td>";
    }
    else
    {
        echo "<td ><p><a href='?page=$j' title='Page $j'>$j</a></p></td>";    
    }
}

if($current_page <= $total_pages - 1){
    $next_page=$current_page+1;
    echo "<td ><p><a href='?page=$next_page'>Next Page</a></p></td>";    
}
else
{
    echo "<td ><p></p></td>";
}        

/*
foreach (glob("entries/*.php") as $filename)
{
    include $filename;
}
*/
?>
</table>
Link to comment
https://forums.phpfreaks.com/topic/287006-only-loading-files-on-specific-pages/
Share on other sites

the code you posted, provided that lines 50-53 are actually commented out, WILL only include the 10 files that correspond to the requested page. if you are seeing more, then either your code isn't what you think or you are seeing a cached version of your page.

 

btw - why do you have a ton of .php pages that you are trying to paginate this way? that indicates you have went to a lot of trouble and time creating and managing php pages instead of just storing the data that's present on these pages in a proper database.

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.