简体   繁体   English

如何跳过我的php脚本的某些部分? (以使我的页面加载速度更快)

[英]How can I skip over certain parts of my php script? (to make my page load faster)

<?php
$rows_per_page = 2;
$cols_per_page = 2;
$image_href = '<a href=/';
$image_links = array('file1/page1>', 'file2/page2>', 'file3/page3>',  
'file4/page4>',   'file5/page5>', 'file6/page6>', 'file6/page6>',  
'file/page7>', 'file/page8>', 'subfile/page9>');
$img_srcs = '<img src="https://s3.amazonaws.com/imagetitle/';
$images = array();

for($i = 1; $i < 10; $i++)
{
    $images[$i] = $i;
}
$image_ending = '.png" height="200" width="200" /></a>';
$image_descriptions = array('<br />something', '<br />description', 
'<br />arbitrary', 'random', '<br />you', '<br />get', '<br />the',  
'<br />idea', '<br />itsdescriptions');  
$total_images = count($images);
$images_per_page = $rows_per_page * $cols_per_page;
$total_images = count($images);
$total_pages = ceil($total_images / $images_per_page);
$current_page = (int) $_GET['page'];
if($current_page<1 || $current_page>$total_pages)
{
    $current_page = 1;
}

//Get records for the current page
$page_image_links = array_splice($image_links, ($current_page-1)*$images_per_page, $images_per_page);
$page_images = array_splice($images, ($current_page-1)*$images_per_page, $images_per_page);
$page_image_descriptions = array_splice($image_descriptions, ($current_page-1)*$images_per_page, $images_per_page);
$slots = "<table border=\"1\">";
for($row=0; $row<$rows_per_page; $row++)
{
    $slots .= "<tr>";
    for($col=0; $col<$cols_per_page; $col++)
    {
        $imgIdx = ($row * $rows_per_page) + $col;
        $img = (isset($page_images[$imgIdx])) ? "{$image_href}{$page_image_links[$imgIdx]}{$img_srcs}{$page_images[$imgIdx]}{$image_ending}{$page_image_descriptions[$imgIdx]}" : '&nbsp;';
        $slots .= "<td>$img</td>";
    }
    $slots .= "</tr>";
}
$slots .= "</table>";

//Create pagination links
$first = "First";
$prev  = "Prev";
$next  = "Next";
$last  = "Last";
if($current_page>1)
{
    $prevPage = $current_page - 1;
    $first = "<a href=\"blah.php?page=1\">First</a>";
    $prev  = "<a href=\"blah.php?page={$prevPage}\">Prev</a>";
}
if($current_page<$total_pages)
{
    $nextPage = $current_page + 1;
    $next = "<a href=\"blah.php?page={$nextPage}\">Next</a>";
    $last = "<a href=\"blah.php?page={$total_pages}\">Last</a>";
}

?>
<html>
<title></title>
<body>
<h2>Here are the records for page <?php echo $current_page; ?></h2>
  <ul>
    <?php echo $slots; ?>
  </ul>
Page <?php echo $current_page; ?> of <?php echo $total_pages; ?>
<br />
<?php echo "{$first} | {$prev} | {$next} | {$last}"; ?>
</body>
</html>

So basically this code above makes it easy to put up images and their links. 因此,基本上以上这段代码使创建图像及其链接变得容易。 For now, I don't have that many images, so the page runs super fast. 现在,我没有那么多图像,因此页面运行非常快。 However, I'm thinking in the long run. 但是,我从长远来看。 If I have say, 10,000 images, it's gonna take a long time to process each paginated page, as I would have $image_links of file1/page1 up to file10000/page10000, and 10000 different image descriptions! 如果我说10,000张图像,处理每个分页页面将花费很长时间,因为我将拥有file1 / page1到$ file10000 / page10000的$ image_links,以及10000个不同的图像描述! Is there a way to stop the web browser from reading, or skip, certain parts of the script (the $image_links and $image_descriptions in my case)? 有没有一种方法可以阻止Web浏览器读取或跳过脚本的某些部分(本例中为$ image_links和$ image_descriptions)? That way, it won't need to read through all 10000 $image_links and $image_descriptions. 这样,它将不需要通读全部10000个$ image_links和$ image_descriptions。

Thanks! 谢谢!

What people usually do in this kind of a situation is to store a record of each image in a database. 人们在这种情况下通常要做的是将每个图像的记录存储在数据库中。 That way you don't have to create the whole list of images each time. 这样,您不必每次都创建整个图像列表。 So you would only retrieve the images for the page that a user is currently viewing. 因此,您将只检索用户当前正在查看的页面的图像。

Caching 高速缓存

You can also cache your results. 您也可以缓存结果。 There are all sorts of methods, classes and libraries to do that, but it basically involves doing something like this: 有各种各样的方法,类和库可以执行此操作,但是基本上涉及到执行以下操作:

$cache_key = 'cache-page-'.$pagenumer.'.cache';
if (!file_exists($cache_key)) {

    ob_start();
    // all your code comes here - to make it very
    // fast, you can do an include() here on the other file
    include 'the-rest-of-your-code-example.php';
    $output = ob_get_clean();
    $fp = fopen($cache_key, 'w');
    fwrite($fp, $output);
    fclose($fp);

} else {
    $output = file_get_contents($cache_key);
}

// And finally echo the content
echo $output;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM