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
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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.