2011-03-14 98 views
0
<?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> 

所以基本上這段代碼可以很容易地放置圖像及其鏈接。目前,我沒有那麼多的圖像,所以頁面運行速度非常快。不過,我從長遠考慮。如果我說,有10,000張圖片,需要很長時間才能處理每個分頁頁面,因爲我會將file1/page1的$ image_links分配到file10000/page10000以及10000個說明!有沒有辦法阻止瀏覽器閱讀或跳過腳本的某些部分(在我的情況下是$ image_links和$ image_descriptions)?這樣,它不需要讀取所有10000 $ image_links和$ image_descriptions。如何阻止我的php腳本的某些部分執行? (使我的頁面加載速度更快)

謝謝!

+0

我不認爲有任何方法可以解決所有圖像鏈接,desc等數組加載問題。您的頁面實際上並沒有循環遍歷每個數組元素;你計算出你需要的項目的索引範圍,然後將這些項目從數組中取出。另一方面,你不會循環它們。不利的一面是,您需要每個項目,以便索引計算正常工作。 – 2011-03-14 03:26:16

回答

2

我要提出另一種方法。爲此使用數組並不理想;它真的覺得它應該是一個數據庫驅動的應用程序。

如果您決定採取這種方法,分頁變得更容易。你可以問你的數據庫引擎的相應記錄。例如,在MySQL中,你會要求這樣的數據:

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

這將返回id的21到30.

......並且,會更好。當然,如果你從來沒有對數據庫做過任何工作,那就有相當的學習曲線。

+0

不,我從來沒有做過數據庫,不幸的是。我認爲我在這裏所做的絕對不需要數據庫。這似乎是一件非常簡單的事情,我無法弄清楚。另外,調用SQL數據庫比運行純php要慢。特別是如果我可以退出某些部分。 – user657847 2011-03-14 03:15:14

0

嘗試使用你的身體<>的HTML在onload功能,所以它會加載一次

+0

我不認爲我使用了正確的措辭。我想我應該說跳過。因爲我認爲我的PHP腳本加載了一堆不必要的東西。 – user657847 2011-03-14 02:56:17

1

如果你想在某一個地方,結束執行,只是調用exit的頁面;

+0

這實際上是否工作+1; – 2011-03-14 02:56:57

+0

這在我的情況下不起作用,我不認爲... – user657847 2011-03-14 03:02:00

相關問題