【发布时间】:2018-05-27 07:56:38
【问题描述】:
我想在我的图片库中添加分页。但它在一页上显示所有图像,而不是在一页上显示 6 个图像。我怎样才能做到这一点?请帮忙
<?php
echo "<html><head><title>Image</title></head><body>";
$rec_limit = 3;
$conn = mysqli_connect("localhost", "root", "test123#", "imagesdatabase") or die("unable to connect");
$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');
$rec_count = 20;
if( isset($_GET{'page'} ) ) {
$page = $_GET{'page'} + 1;
$offset = $rec_limit * $page ;
}else {
$page = 0;
$offset = 0;
}
$left_rec = $rec_count - ($page * $rec_limit);
$url = "www.testwebsite.com". $_SERVER['PHP_SELF'] ;
$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");
while ( $row=mysqli_fetch_array($entity_ids,MYSQLI_ASSOC)) {
//print_r($row);
$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"/></a>';
}
echo "<br>";
if( $page > 0 ) {
$last = $page - 2;
echo "<a href = \"$url?page = $last\">Last 10 Records</a> |";
echo "<a href = \"$url?page = $page\">Next 10 Records</a>";
}else if( $page == 0 ) {
echo "<a href = \"$url?page = $page\">Next 10 Records</a>";
}else if( $left_rec < $rec_limit ) {
$last = $page - 2;
echo "<a href = \"$url?page = $last\">Last 10 Records</a>";
}
//mysql_close($conn);
echo "</body></html>";
图片显示正确,但我只想添加分页并在每页上仅显示 6 张图片。
任何帮助将不胜感激。谢谢
【问题讨论】:
-
您需要在查询中使用
LIMIT子句,并根据您的页码进行偏移。 -
请注意,多次列出相同的 entity_id 是没有意义的。已更正。
-
鉴于您永远不可能拥有超过几十张图片,我很想在 php/JavaScript 中处理所有分页
-
@Strawberry 仅用于测试目的。我现在已经更新了我的查询.. 请检查
-
天哪。您已撤消所有格式设置。快的。投反对票。
标签: javascript php html mysql ajax