【问题标题】:Implementing pagination in PHP在 PHP 中实现分页
【发布时间】:2019-09-19 20:21:17
【问题描述】:

如何在这段代码中实现分页:

<?php
    include "connect.php";
    $query  = mysql_query("SELECT username, date FROM users ORDER BY id ASC");
    while($row  = mysql_fetch_array($query)):
?>
        <div><h5><?php echo $row['username']; ?> (<?php echo $row['date']; ?>)</h5></div>
<?php endwhile; ?>

【问题讨论】:

标签: php mysql pagination


【解决方案1】:

这个想法是使用 mysql LIMIT 关键字。它可以让您限制从 mysql 获得的结果数量。

SELECT data FROM table LIMIT 5 

等价于

SELECT data FROM table LIMIT 0, 5

其中 0 是偏移量,5 是 MySQL 返回的行数。

所以你必须修复一些要显示的项目,比如$numRes = 10 和页码:

if (isset($_GET['page']) && is_numeric($_GET['page']))
     $page = $_GET['page'];
else
     $page = 0;

所以你的要求是这样的:

sprintf($request, "SELECT data FROM pages LIMIT %d, %d", $page, $numRes);

【讨论】:

  • 偏移量是页码还是起始值,如(page number * $numRes)?
  • 这是起始值,因为您的公式描述了@Flipper。
【解决方案2】:
class Pagination
{
    public $cur_page;
    public $total;
    public $per_page;

    function __construct($cur_page, $total, $per_page)
    {
        $this->cur_page = $cur_page;
        $this->total = $total;
        $this->per_page = $per_page;
    }

    function getTotalPage(){
        return ceil($this->total / $this->per_page);
    }

    function hasPrevPage(){
        if($this->cur_page > 1){
            return true;
        }
        else{
            return false;
        }
    }

    function hasNextPage(){
        if($this->cur_page < $this->getTotalPage()){
            return true;
        }
        else{
            return false;
        }
    }

    function offSet(){
        return ($this->cur_page - 1) * $this->per_page;
    }
}

$total = 12;
$per_page = 5;
$cur_page = 1;

$pagination = new Pagination($cur_page, $total, $per_page);
$offSet = $pagination->offSet();
$query = "SELECT username, date FROM users ORDER BY id ASC LIMIT {$offset}, {$per_page}";

【讨论】:

    【解决方案3】:
    <?php
        require_once "connect.php";
        define('PER_PAGE',5);
        if (isset($_GET['page']) && is_numeric($_GET['page'])):  // in your href use "domain.com/index.php?page=1"
            $start = $_GET['page_name'] * PER_PAGE;
            $query  = mysql_query("SELECT username, date FROM users ORDER BY id ASC LIMIT {$start},{$per_page}"); // where to start and for how many 1-5
            while ($row = mysql_fetch_array($query)): ?>
                <div><h5><?php echo $row['username']; ?> (<?php echo $row['date']; ?>)</h5></div>
                <?php
            endwhile;
        endif;
    ?>
    

    【讨论】:

    • 但是如何在没有 url 的情况下更改页码
    【解决方案4】:

    PHP/MySQL 中的分页可以通过使用LIMIT 子句来实现。

    最初,您必须计算总记录数并将其除以每页的记录数。将页码与 REQUEST 一起传递。

    从REQUEST数组中读取页码,找出该页的起始记录。

    您可以在下一页http://www.csnotes32.com/2014/11/page-wise-or-paginated-listing-of-data.html进一步阅读并查看完整的实现

    【讨论】:

      【解决方案5】:

      这是分页代码。将此与您的数据库一起使用,您将获得 Google 风格的分页:

       <?php
      
        $conn=mysqli_connect("localhost","root","","ui");
      
      
        $start=0;
         $limit=5;
      
            $t=mysqli_query($conn,"select * from form_table");
            $total=mysqli_num_rows($t);
      
      
      
             if(isset($_GET['id']))
             {
                  $id=$_GET['id'] ; 
                                  $start=($id-1)*$limit;
      
                                    }
                      else
                      {
                  $id=1;
         }
           $page=ceil($total/$limit);
      
             $query=mysqli_query($conn,"select * from form_table limit                                        $start,$limit");
           ?>
        <!DOCTYPE html>
      <html>
      <head>
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="stylesheet"       href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
        <script s        src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js">     </script>
           <script                    src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js">          </script>
            </head>         
           <body>
      
          <div class="container">
           <h2>Table</h2>
              <table class="table table-bordered">
              <thead>
                <tr>
                 <th>Id</th>
                   <th>Name</th>
                 <th>Gender</th>
      
      
          <th>Hobbies</th>
          <th>Course</th>
        </tr>
        </thead>
          <tbody>
      
      <?php
      while($ft=mysqli_fetch_array($query))
      {?>
       <tr>
          <td><?= $ft['0']?></td>
          <td><?= $ft['1']?></td>
          <td><?= $ft['2']?></td>
          <td><?= $ft['3']?></td>
          <td><?= $ft['4']?></td>
        </tr>
      <?php
      }
      
      ?>
      
      
      </tbody>
        </table>
           <ul class="pagination">
       <?php if($id > 1) {?> <li><a href="?id=<?php echo ($id-1) ?       >">Previous</a></li><?php }?>
       <?php
       for($i=1;$i <= $page;$i++){
       ?>
          <li><a href="?id=<?php echo $i ?>"><?php echo $i;?></a></li>
        <?php
       }
        ?>
      <?php if($id!=$page)
      //3!=4
      {?> 
      

      【讨论】:

        【解决方案6】:

        这是php##中的分页代码

        <!DOCTYPE html>
        <html>
        <head>
        <link rel="stylesheet" id="font-awesome-style-css" href="https://www.phpflow.com/code/css/bootstrap3.min.css" type="text/css" media="all">
        <!-- jQuery -->
        <script type="text/javascript" charset="utf8" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.2.min.js"></script>
        <title>phpflow.com : Source code of simaple ajax pagination</title>
        </head>
        <body>
        <div><h3>Source code : PHP simaple ajax pagination</h1></div>
        <div>
        <div id="target-content" >loading...</div>
        
        <?php
        include('db.php'); 
        $limit = 4;
        $sql = "SELECT COUNT(id) FROM coffee";  
        $rs_result = mysqli_query($conn, $sql);  
        $row = mysqli_fetch_row($rs_result);  
        $total_records = $row[0];  
        $total_pages = ceil($total_records / $limit); 
        ?>
        <div align="center">
        <ul id="pagination">
        <?php if(!empty($total_pages)):for($i=1; $i<=$total_pages; $i++):  
              if($i == 1):?>
                    <h3 class='active'  id="<?php echo $i;?>" style="float: left;"><a href='pagination.php?page=<?php echo $i;?>' ><?php echo $i;?></a></h3> 
              <?php else:?>
              <h3 id="<?php echo $i;?>" style="float: left;"><a href='pagination.php?page=<?php echo $i;?>'><?php echo $i;?></a></h3>
            <?php endif;?>      
        <?php endfor;endif;?>  
        </div>
        </div>
        </body>
        <script>
        jQuery(document).ready(function() {
        jQuery("#target-content").load("pagination.php?page=1");
            jQuery("#pagination h3").live('click',function(e){
          e.preventDefault();
            jQuery("#target-content").html('loading...');
            jQuery("#pagination h3").removeClass('active');
            jQuery(this).addClass('active');
                var pageNum = this.id;
                jQuery("#target-content").load("pagination.php?page=" + pageNum);
            });
            });
        </script>

        ##

        【讨论】:

        • 这是一个使用 mysql 在 php 中工作的代码分页,您可以根据您的表格进行更改
        猜你喜欢
        • 2020-04-11
        • 1970-01-01
        • 2010-09-21
        • 1970-01-01
        • 2016-07-23
        • 2019-02-27
        • 2015-03-22
        相关资源
        最近更新 更多