每页显示一定数量的图像。我想设置任何页面上显示的图像数量的最大限制,例如设置一个属性,使其不超过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>";
}
答案 0 :(得分:2)
最好的方法是从数据库中仅获取8个图像,而不是一次获取所有图像。使用分页设置每个页面的限制和偏移量。
答案 1 :(得分:1)
您需要添加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>';
然后是下一个按钮
{{1}}
这是分页背后的基本理念。