duancenxiao0482 2011-03-14 02:51
浏览 20
已采纳

如何阻止我的PHP脚本的某些部分执行? (使我的页面加载速度更快)

<?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 descriptions! 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)? That way, it won't need to read through all 10000 $image_links and $image_descriptions.

Thanks!

  • 写回答

3条回答 默认 最新

  • doutangliang7769 2011-03-14 03:07
    关注

    I'm going to suggest another approach. Using arrays for this is not ideal; it really feels like it should be a database driven app.

    If you decide to go for that approach, pagination becomes much easier. You can just ask your database engine for the corresponding records. For example, in MySQL, you'd ask for the data something like this:

    select * from tableA order by id limit 10 offset 20;

    This would return id's 21 thru 30.

    ... and, would scale much better. Of course, if you've never done any work with databases, there is quite a learning curve.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,