在PHP中实现分页

时间:2011-03-30 17:42:02

标签: php mysql pagination

如何在此代码中实现分页:

<?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; ?>

6 个答案:

答案 0 :(得分:3)

想法是使用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);

答案 1 :(得分:1)

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}";

答案 2 :(得分:0)

<?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;
?>

答案 3 :(得分:0)

使用LIMIT子句可以简单地实现PHP / MySQL中的分页。

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

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

您可以在以下页面http://www.csnotes32.com/2014/11/page-wise-or-paginated-listing-of-data.html

中进一步阅读并查看完整的实施

答案 4 :(得分:0)

这是分页代码。将其与您的数据库一起使用,您将获得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
{?> 

答案 5 :(得分:0)

这是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>

##