如何在图库中添加PHP分页

时间:2018-05-27 07:56:39

标签: javascript php html mysql ajax

我想在我的图片库中添加分页。但是它在一个页面中显示所有图像而不是在一个页面上显示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张图片。

任何帮助将不胜感激。谢谢

1 个答案:

答案 0 :(得分:0)

经过这么多的耐心,我已经理解了添加分页和使用引导分页的每个标准..

以下是我的回答。希望它会帮助某人。

<!DOCTYPE html>
<html>
<head>
    <title>Images Grid View</title>
    <style type="text/css">
        #thumb {
clear : both;
width : 100%;
margin-left : 0;
}
#thumb ul {
width : 100%;
}
#thumb ul li {
display : inline;
font-family : arial;
float : left;
padding-right : 5px;
width: 210px; 
height : 280px;
}
#thumb ul li img {
float : left;
width : 200px;
height : 200px;
border : #ccc solid 1px;
padding : 2px;
}
</style>
</head>
<body>



<?php
$conn = mysqli_connect("localhost", "root", "test123#", "imagesdatabase") or die("unable to connect");
$rootPath = '/htdocs/stage6/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_limit = 6;
         $rec_count = 50; 
         if (isset($_GET['pageno'])) {
             $pageno = $_GET['pageno'];
         } else {
             $pageno = 1;
         }
         $no_of_records_per_page = 12;
         $offset = ($pageno-1) * $no_of_records_per_page; 

         $total_pages_sql = "SELECT count(e.entity_id) 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";
         $result = mysqli_query($conn,$total_pages_sql);
         $total_rows = mysqli_fetch_array($result)[0];
         $total_pages = ceil($total_rows / $no_of_records_per_page);

         $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  $offset, $no_of_records_per_page");
         echo '<div id="thumb"><ul>';
         while ( $row=mysqli_fetch_array($entity_ids,MYSQLI_ASSOC)) {
            $entity_id = $row['entity_id'];
            $image = $row['value'];
            echo '<li><p>' . $entity_id .'</p>';

            echo '<img src="https://testwebsite.com/pub/media/catalog/product/'.$image.'" alt="Image" /></a>';
            echo '</li>';
         }
         echo '</ul></div>';

?>


<center>
<ul class="pagination" style="list-style-type:none; display:-webkit-inline-box !important; float: left; font-size: 24px;">
    <li style="background-color:gray;"><a href="?pageno=1">First</a></li>
    <li style="background-color:gray;" class="<?php if($pageno <= 1){ echo 'disabled'; } ?>">
        <a href="<?php if($pageno <= 1){ echo '#'; } else { echo "?pageno=".($pageno - 1); } ?>">Prev</a>
    </li>
    <li style="background-color:gray;" class="<?php if($pageno >= $total_pages){ echo 'disabled'; } ?>">
        <a href="<?php if($pageno >= $total_pages){ echo '#'; } else { echo "?pageno=".($pageno + 1); } ?>">Next</a>
    </li>
    <li style="background-color:gray;"><a href="?pageno=<?php echo $total_pages; ?>">Last</a></li>
</ul>
</center>

</body>
</html>