【问题标题】:How can i make the pagination?我怎样才能使分页?
【发布时间】:2020-10-30 18:57:13
【问题描述】:

我想对数据库中的图片进行分页。我试过的任何东西都不起作用,我不知道该怎么办。有人可以为我解释一下吗?看了很多教程,仍然找不到解决方案(我是初学者)。

<?php
// Connect to MySQL
$pdo = pdo_connect_mysql();
// MySQL query that selects all the images
$stmt = $pdo->query('SELECT * FROM images ORDER BY uploaded_date DESC');
$images = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>


    <div class="content home">
    <h2>Gallery</h2>
    <p>Welcome to the gallery page, you can view the list of images below.</p>

    <div class="images">
        <?php foreach ($images as $image): ?>
        <?php if (file_exists($image['path'])): ?>
        <a href="#">
            <img src="<?=$image['path']?>" alt="<?=$image['description']?>" data-id="<?=$image['id']?>" data-title="<?=$image['title']?>" width="300" height="200">
            <span><?=$image['description']?></span>
        </a>
        <?php endif; ?>
        <?php endforeach; ?>
    </div>


    <div class="container">
  <ul class="pagination">
    <li class="page-item"><a class="page-link" href="#">Previous</a></li>
    <li class="page-item"><a class="page-link" href="#">1</a></li>
    <li class="page-item"><a class="page-link" href="#">2</a></li>
    <li class="page-item"><a class="page-link" href="#">3</a></li>
    <li class="page-item"><a class="page-link" href="#">Next</a></li>
  </ul>
</div>


</div>

【问题讨论】:

  • ????如果您刚刚开始使用 PHP 并想构建应用程序,我强烈建议您查看各种 development frameworks 以查看是否可以找到适合您的风格和需要。它们有多种风格,从 Fat-Free Framework 这样的轻量级到像 Laravel 这样的更全面。这些为您提供了具体的示例,以及如何编写代码和组织项目文件的指导。分页是一个已解决的问题。
  • 您尝试过什么,出了什么问题?您现在发布的代码只是一个显示所有内容的普通页面

标签: php mysql pagination


【解决方案1】:

LIMIT 和 OFFSET 的样子

SELECT * FROM images ORDER BY uploaded_date DESC LIMIT 5 OFFSET 10

OFFSET 值会产生页面的效果。 OFFSET 0 是第 1 页 OFFSET 5 是第 2 页 OFFSET 10 是第 3 页等.....

【讨论】:

    【解决方案2】:

    php 中的分页

    <?php  
        $limit = 3;  //set  Number of entries to show in a page.
        // Look for a GET variable page if not found default is 1.        
        if (isset($_GET["page"])) {    
        $page  = $_GET["page"];    
        }    
        else { $page=1;    
        } 
        //determine the sql LIMIT starting number for the results on the displaying page  
        $page_index = ($page-1) * $limit;      // 0
    
        $All_Users=mysqli_query($con,"select * from users limit $page_index, $limit");
        while($row=mysqli_fetch_array($All_Users))
        {
            //show  data in table or where you want..
        }
        $all_data=mysqli_query($con,"select count(*) from users");
        $user_count = mysqli_fetch_row($all_data);   //total count 9  
        $total_records = $user_count[0];   //9
        $total_pages = ceil($total_records / $limit);    // 9/3=  3
        if($page >= 2){
            echo "<a href='blog.php?page=".($page-1)."' class='btn customBtn2'>Previous</a>";
          }
        
        if($page<$total_pages) {
            echo "<a href='blog.php?page=".($page+1)."' class='btn customBtn2'>NEXT</a>";   
        }       
    ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-23
      • 2022-01-23
      • 1970-01-01
      • 1970-01-01
      • 2023-01-28
      • 1970-01-01
      • 1970-01-01
      • 2021-09-30
      相关资源
      最近更新 更多