What potential issue could arise when trying to display a large number of images in a PHP script?
One potential issue that could arise when trying to display a large number of images in a PHP script is memory exhaustion due to loading all the images at once. To solve this issue, you can use pagination to limit the number of images loaded and displayed on each page, reducing the memory usage.
<?php
// Define the number of images to display per page
$imagesPerPage = 10;
// Get the current page number
$page = isset($_GET['page']) ? $_GET['page'] : 1;
// Calculate the offset for the images to display
$offset = ($page - 1) * $imagesPerPage;
// Query the database or fetch the images from a directory
$images = // Fetch images from database or directory
// Display images based on the offset and limit
for ($i = $offset; $i < min($offset + $imagesPerPage, count($images)); $i++) {
echo '<img src="' . $images[$i] . '" alt="Image ' . ($i + 1) . '">';
}
// Display pagination links
$totalPages = ceil(count($images) / $imagesPerPage);
for ($i = 1; $i <= $totalPages; $i++) {
echo '<a href="?page=' . $i . '">' . $i . '</a> ';
}
?>