2013-01-22 52 views
2

您好我有25行MySql表,我想顯示前10頁使用PHP的頁面..當我點擊下一個按鈕時,它顯示了另外10行從表等在...幫助我。 謝謝。Php代碼動態顯示來自mysql的10條記錄

+2

谷歌「在PHP分頁」 –

+0

除非處理大的結果集,或數據不斷將延長,使得往返於數據庫的開銷可能會比只肥了整個結果到更大一個數組/ json/xml,並完全在php/javascript中處理分頁。 – Strawberry

回答

1

樣品分頁代碼如下所示。

試試吧。

<?php 

    include('config.php'); // include your code to connect to DB. 
    $tbl_name="";  //your table name 
    $adjacents = 3; 

    $query = "SELECT COUNT(*) as num FROM $tbl_name"; 
    $total_pages = mysqli_fetch_array(mysqli_query($query)); 
    $total_pages = $total_pages[num]; 


    $targetpage = "filename.php"; //your file name (the name of this file) 
    $limit = 10;        //how many items to show per page 
    $page = $_GET['page']; 
    if($page) 
     $start = ($page - 1) * $limit;   //first item to display on this page 
    else 
     $start = 0;        //if no page var is given, set start to 0 

    /* Get data. */ 
    $sql = "SELECT column_name FROM $tbl_name LIMIT $start, $limit"; 
    $result = mysqli_query($sql); 

    /* Setup page vars for display. */ 
    if ($page == 0) $page = 1;     //if no page var is given, default to 1. 
    $prev = $page - 1;       //previous page is page - 1 
    $next = $page + 1;       //next page is page + 1 
    $lastpage = ceil($total_pages/$limit);  //lastpage is = total pages/items per page, rounded up. 
    $lpm1 = $lastpage - 1;      //last page minus 1 

    /* 
     Now we apply our rules and draw the pagination object. 
     We're actually saving the code to a variable in case we want to draw it more than once. 
    */ 
    $pagination = ""; 
    if($lastpage > 1) 
    { 
     $pagination .= "<div class=\"pagination\">"; 
     //previous button 
     if ($page > 1) 
      $pagination.= "<a href=\"$targetpage?page=$prev\">? previous</a>"; 
     else 
      $pagination.= "<span class=\"disabled\">? previous</span>"; 

     //pages 
     if ($lastpage < 7 + ($adjacents * 2)) //not enough pages to bother breaking it up 
     { 
      for ($counter = 1; $counter <= $lastpage; $counter++) 
      { 
       if ($counter == $page) 
        $pagination.= "<span class=\"current\">$counter</span>"; 
       else 
        $pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";     
      } 
     } 
     elseif($lastpage > 5 + ($adjacents * 2)) //enough pages to hide some 
     { 
      //close to beginning; only hide later pages 
      if($page < 1 + ($adjacents * 2))  
      { 
       for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++) 
       { 
        if ($counter == $page) 
         $pagination.= "<span class=\"current\">$counter</span>"; 
        else 
         $pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";     
       } 
       $pagination.= "..."; 
       $pagination.= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>"; 
       $pagination.= "<a href=\"$targetpage?page=$lastpage\">$lastpage</a>";  
      } 
      //in middle; hide some front and some back 
      elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2)) 
      { 
       $pagination.= "<a href=\"$targetpage?page=1\">1</a>"; 
       $pagination.= "<a href=\"$targetpage?page=2\">2</a>"; 
       $pagination.= "..."; 
       for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++) 
       { 
        if ($counter == $page) 
         $pagination.= "<span class=\"current\">$counter</span>"; 
        else 
         $pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";     
       } 
       $pagination.= "..."; 
       $pagination.= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>"; 
       $pagination.= "<a href=\"$targetpage?page=$lastpage\">$lastpage</a>";  
      } 
      //close to end; only hide early pages 
      else 
      { 
       $pagination.= "<a href=\"$targetpage?page=1\">1</a>"; 
       $pagination.= "<a href=\"$targetpage?page=2\">2</a>"; 
       $pagination.= "..."; 
       for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++) 
       { 
        if ($counter == $page) 
         $pagination.= "<span class=\"current\">$counter</span>"; 
        else 
         $pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";     
       } 
      } 
     } 

     //next button 
     if ($page < $counter - 1) 
      $pagination.= "<a href=\"$targetpage?page=$next\">next ?</a>"; 
     else 
      $pagination.= "<span class=\"disabled\">next ?</span>"; 
     $pagination.= "</div>\n";  
    } 
?> 

    <?php 
     while($row = mysqli_fetch_array($result)) 
     { 

     // Your while loop here 

     } 
    ?> 

<?=$pagination?>
+0

請不要使用'mysql_ *'作爲示例。至少添加一個「我」。 – itachi

1

使用極限聲明中查詢.... 你需要使用分頁用PHP ..

+0

但您不需要使用限制:-) – Strawberry

+0

但是在處理分頁時,大多數情況下在查詢中使用LIMIT子句 – TNK

2

很廣泛的問題,但基本是它可以與「限制」來解決MySQL命令。基本上你的PHP代碼檢測是否有人去了另一個頁面,然後當它進行查詢時,「LIMIT」被調整。因此,對於第1頁的「限制」在此MySQL的僞代碼將是:

SELECT * FROM {my table} WHERE {criteria is this} LIMIT 0,10 

而對於第2頁的「限制」會是這樣的:

SELECT * FROM {my table} WHERE {criteria is this} LIMIT 11,20 

對於PHP分頁,你將不得不提出一些邏輯。通過POST也許和大多數傳統傳遞一個URL變量如:

http://localhost/index.php?pn=2 

那麼你的PHP邏輯做一些數學來確定範圍的起始&結束。因此,在這個例子中,也許這樣做:

$pn = $GET['pn']; 
$per_page = 10; 
$bottom_limit = (($pn*$per_page)-$per_page)+1); 
$top_limit = ($pn*$per_page); 

然後在PHP的限制將沿着線的放置:

$limit = " LIMIT " . $bottom_limit . ", " . $top_limit;