如何为图片画廊添加PHP分页,实现每页显示6张图片
Got it, let's fix your pagination step by step. The main issues were hardcoded record counts, missing LIMIT/OFFSET in your query, incorrect page calculation, and wrong link text/logic. Here's the corrected code with explanations:
Corrected Code
<?php echo "<html><head><title>Image</title></head><body>"; // Set number of images per page (updated to 6) $rec_limit = 6; // Database connection $conn = mysqli_connect("localhost", "root", "test123#", "imagesdatabase") or die("unable to connect"); // Magento initialization (kept as is since your images load correctly) $rootPath = '/var/www/html/'; require_once $rootPath.'app/bootstrap.php'; $bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER); $objManager = $bootstrap->getObjectManager(); $state = $objManager->get('\Magento\Framework\App\State'); $state->setAreaCode('frontend'); $resource = $objManager->get('\Magento\Framework\App\ResourceConnection'); $connection = $resource->getConnection('core_write'); // Calculate total number of images (dynamic instead of hardcoding 20) $count_query = "SELECT COUNT(*) as total FROM catalog_product_entity_media_gallery g JOIN catalog_product_entity_media_gallery_value v ON (g.value_id = v.value_id) JOIN catalog_product_entity e ON (v.entity_id = e.entity_id) WHERE e.attribute_set_id = 62"; $count_result = mysqli_query($conn, $count_query); $count_row = mysqli_fetch_assoc($count_result); $rec_count = $count_row['total']; // Handle current page (start at 0 for first page) $page = isset($_GET['page']) ? (int)$_GET['page'] : 0; $offset = $page * $rec_limit; // Fetch only images for the current page using LIMIT and OFFSET $entity_ids = mysqli_query($conn,"SELECT e.entity_id,g.value_id,g.value from catalog_product_entity_media_gallery g join catalog_product_entity_media_gallery_value v on (g.value_id = v.value_id) join catalog_product_entity e on (v.entity_id = e.entity_id) where e.attribute_set_id = 62 LIMIT $rec_limit OFFSET $offset"); // Display images (fixed stray </a> tag) while ($row=mysqli_fetch_array($entity_ids,MYSQLI_ASSOC)) { $entity_id = $row['entity_id']; $image = $row['value']; echo $entity_id; echo '<img src="www.testwebsite.com/pub/media/catalog/product/'.$image.'" alt="Image" width="200px" height="200px"/>'; } echo "<br>"; // Pagination links $url = "www.testwebsite.com" . $_SERVER['PHP_SELF']; // Previous page link (only visible if not on first page) if ($page > 0) { $prev_page = $page - 1; echo "<a href=\"$url?page=$prev_page\">Previous 6 Images</a> | "; } // Next page link (only visible if more images exist) if ($offset + $rec_limit < $rec_count) { $next_page = $page + 1; echo "<a href=\"$url?page=$next_page\">Next 6 Images</a>"; } // Close database connection mysqli_close($conn); echo "</body></html>"; ?>
Key Changes Made:
- Per-page limit update: Changed
$rec_limitfrom 3 to 6 to match your requirement of 6 images per page. - Dynamic total count: Replaced the hardcoded
$rec_count = 20with a COUNT query to get the actual number of images in your database. This ensures pagination adapts if you add/remove images later. - LIMIT/OFFSET in main query: This tells MySQL to only load the images for the current page, which fixes the issue of all images showing on one page.
- Simplified page calculation: Restructured how we handle the current page to start at 0, making offset calculation straightforward and avoiding logic errors.
- Improved pagination links:
- Only show "Previous" when you're not on the first page.
- Only show "Next" when there are more images beyond the current page.
- Updated link text to correctly reference 6 images instead of 10.
- Fixed invalid HTML: Removed the stray
</a>tag that had no matching opening tag in your image display code.
This should now properly paginate your image gallery with 6 images per page.
内容的提问来源于stack exchange,提问作者Rahul
相关产品推荐
相关产品推荐

