2011-03-30 72 views
-1

如何在這段代碼實現分頁:實現分頁的PHP

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

的問題有很多可能的複製[在PHP分頁](的 – pt2ph8 2011-03-30 17:54:42

+0

可能重複http://stackoverflow.com/questions/4857663/pagination- in-php) – 2011-03-30 18:23:57

回答

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); 
+0

偏移量是頁碼還是起始值,如(頁碼* $ numRes)? – Flipper 2011-03-30 18:43:41

+0

這是初始值,因爲您的Formule描述了@Flipper。 – Aif 2011-03-30 23:23:05

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

但我怎麼能改變沒有網址的頁碼 – q7eb2a 2011-03-30 18:20:59

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

這是分頁代碼的完整實現。與您的數據庫使用此,您將得到谷歌的風格分頁:

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