简体   繁体   中英

How can I limit the number of images shown per page?

I have a certain number of images that are displaying per page. I want to set a maximum limit for the number of images shown on any page, for example setting a property such that no more than eight are shown. I've written this logic in PHP but I'm still seeing all images showing on any given page, ignoring any limit I set. The code:

$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个图像,而不是一次获取所有图像。使用分页设置每个页面的限制和偏移量。

You need to add a $_GET['variable'] ;

Something like this,

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>";

}

Then next button

echo '<a href="myurl.com?last_image='. $counter .'">Next</a>';

This is the basic idea behind a pagination.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM