![](/img/trans.png)
[英]How can i limit the number of articles (retrieved from an RSS) shown in php per page ? (Eg 10 articles per page)
[英]How can I limit the number of images shown per page?
我每頁顯示一定數量的圖像。 我想設置任何頁面上顯示的圖像數量的最大限制,例如設置一個屬性,使其不超過8。 我在PHP中編寫了這個邏輯,但我仍然看到所有圖像都顯示在任何給定的頁面上,忽略了我設置的任何限制。 編碼:
$counter = 0;
foreach ($device as $value) {
$entry = $value;
echo "<head>";
echo '<script type="text/javascript">',
'window.setInterval(function() { ',
"document.getElementById('$counter').src='/latimage.php?&dev=$entry&random='+new Date().getTime();",
'},1000)',
'</script>';
echo "</head>";
echo "<body onLoad='setTimeout('refresh()',1000)'>";
echo "<td>$entry<img id= '$counter' width='100%' height='auto'></img></td>";
$counter = $counter + 1;
if ($counter == 4 || $counter == 8) {
echo " <tr>";
}
最好的方法是從數據庫中僅獲取8個圖像,而不是一次獲取所有圖像。使用分頁設置每個頁面的限制和偏移量。
你需要添加一個$_GET['variable']
;
像這樣的東西,
if(isset($_GET['last_image'])) {
$last_image = $_GET['last_image'];
}
else {
$last_image = 0;
}
$device[] = //however you get the images
$size_of_array = count($device);
for ($counter = $last_image; $counter < $counter + 8 && $counter < $size_of_array; $counter++) {
$entry = $device[$counter];
echo "<head>";
echo '<script type="text/javascript">',
'window.setInterval(function() { ',
"document.getElementById('$counter').src='/latimage.php?&dev=$entry&random='+new Date().getTime();",
'},1000)',
'</script>';
echo "</head>";
echo "<body onLoad='setTimeout('refresh()',1000)'>";
echo "<td>$entry<img id= '$counter' width='100%' height='auto'></img></td>";
}
然后下一個按鈕
echo '<a href="myurl.com?last_image='. $counter .'">Next</a>';
這是分頁背后的基本思想。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.