如何跳过php脚本的某些部分?(使我的页面加载更快) 这是第页的记录 第页,共页

如何跳过php脚本的某些部分?(使我的页面加载更快) 这是第页的记录 第页,共页,php,pagination,Php,Pagination,所以基本上,上面的代码可以很容易地建立图像和它们的链接。现在,我没有那么多的图片,所以页面运行得非常快。然而,从长远来看,我会考虑。如果我有10000张图片,处理每个分页的页面需要很长时间,因为我会有$image\u链接的file1/page1到file10000/page10000,以及10000个不同的图片描述!有没有办法阻止web浏览器读取或跳过脚本的某些部分(在我的例子中是$image\u链接和$image\u描述)?这样,它就不需要阅读所有10000美元的图片链接和图片描述 谢谢 在这

所以基本上,上面的代码可以很容易地建立图像和它们的链接。现在,我没有那么多的图片,所以页面运行得非常快。然而,从长远来看,我会考虑。如果我有10000张图片,处理每个分页的页面需要很长时间,因为我会有$image\u链接的file1/page1到file10000/page10000,以及10000个不同的图片描述!有没有办法阻止web浏览器读取或跳过脚本的某些部分(在我的例子中是$image\u链接和$image\u描述)?这样,它就不需要阅读所有10000美元的图片链接和图片描述


谢谢

在这种情况下,人们通常会在数据库中存储每个图像的记录。这样,您就不必每次都创建整个图像列表。因此,您只能检索用户当前正在查看的页面的图像

缓存 您还可以缓存结果。有各种各样的方法、类和库可以做到这一点,但它基本上包括这样做:

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

在这种情况下,人们通常会在数据库中存储每个图像的记录。这样,您就不必每次都创建整个图像列表。因此,您只能检索用户当前正在查看的页面的图像

缓存 您还可以缓存结果。有各种各样的方法、类和库可以做到这一点,但它基本上包括这样做:

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

这看起来不是很强迫吗?我现在做的似乎不足以保证数据库的使用。如果你期望有10000张图片,这是正确的方法。如果您的期望值超过25行,那么这可能是正确的方法。这比首先定义一个包含10k行的数组,然后检索其中的一部分要快得多,也要容易得多。很抱歉,这么晚才响应。不管怎样,我想我会建立一个数据库。。。希望学习曲线不要太陡。而且,我认为缓存是由浏览器自动完成的!是否有提供有关缓存的简单教程的地方?关于你的代码,我只是把我的一大块php放在那里,对吗?顺便说一句,提前谢谢。有很多级别的缓存。大规模web应用程序在多个级别上实现了缓存。数据库层、数据检索层、html输出层等都有缓存。。。我写的缓存是在html输出层上缓存的——它只会在脚本没有生成缓存文件时让脚本运行——如果生成了缓存文件,它就不会包含你的大数组文件,等等。。浏览器级别的缓存只是为了确保用户不会一次又一次地下载相同的图像/css,这也是非常重要的(但与您的问题无关),这看起来不是很强迫吗?我现在做的似乎不足以保证数据库的使用。如果你期望有10000张图片,这是正确的方法。如果您的期望值超过25行,那么这可能是正确的方法。这比首先定义一个包含10k行的数组,然后检索其中的一部分要快得多,也要容易得多。很抱歉,这么晚才响应。不管怎样,我想我会建立一个数据库。。。希望学习曲线不要太陡。而且,我认为缓存是由浏览器自动完成的!是否有提供有关缓存的简单教程的地方?关于你的代码,我只是把我的一大块php放在那里,对吗?顺便说一句,提前谢谢。有很多级别的缓存。大规模web应用程序在多个级别上实现了缓存。数据库层、数据检索层、html输出层等都有缓存。。。我写的缓存是在html输出层上缓存的——它只会在脚本没有生成缓存文件时让脚本运行——如果生成了缓存文件,它就不会包含你的大数组文件,等等。。浏览器级别的缓存只是确保用户不会一次又一次地下载相同的图像/css,这一点非常重要(但与您的问题无关)