2017-03-16 25 views
0

我從數據庫中提取用戶名和標識。我有超過500個用戶名。我必須顯示分頁編號,只有1到5,最後是分頁編號。所有分頁編號使用PHP和Mysql顯示

示例: - 分頁編號爲: - 1 2 3 4 5 ... 20(最後一個數字)。

現在我正在水平獲取所有數字。你能幫我嗎?

任何人都可以幫我用下一個和最後一個分頁?

include('../db/connection.php'); 
    $reclimit = 3; 
    if(isset($_GET['page'])){ 
     $page = $_GET['page']; 
    } else { 
     $page = 1; 
    } 

    $start = (($page-1) * $reclimit); 
    $sql = "SELECT * FROM request"; 

    $records =$conn->query($sql);; 
    $total = $records->num_rows; 
    $tpages = ceil($total/$reclimit); 

    $search_sql="SELECT * FROM request LIMIT ".$start."," .$reclimit; 
    $search_result = $conn->query($search_sql); 

HTML

<body> 
    <?php 

     if (isset($search_result->num_rows) > 0) { 
     ?> 
     <h2 class="result-title">Results matching your need</h2> 
     <?php 
      while($search_ok = $search_result->fetch_assoc()) { 
        $user_id=$search_ok['Id']; 
        $user_name=$search_ok['Name']; 

     echo " 
      <div class='search-section'> 
        <div class='search-profile'> 

        <div class='s_user_id'>{$user_id}</div> 
        <div class='s_user_name'>{$user_name}</div> 

        </div> 
        </div> 
     "; 

     }} 
     for($i=1;$i<=$tpages;$i++) { 
         echo "<a href=page.php?page=".$i.">".$i."</a>"; 
        } 
       ?> 



    </body> 
+0

你說的「我的意思是什麼水平獲取所有數字「? –

+0

意味着我得到號碼1,2,3,4,5,6,7,8,9,10,11 ..等等。我需要像1,2,3,4,5 ... 20(這是最後一個號碼) –

回答

0

這是一個簡單的想法,但我沒有測試它。編輯的foreach顯示數字:

$pgStart = 1; 
if (isset($_GET['page'])) { // get first showing number = current page - 2 
    $pg = $_GET['page'] - 2; 
    $pgStart = $pg + 5 > $tpages ? $tpages - 4 : $pg; //EDIT fix when reach pages end 
    $pgStart = $pg < 1 ? 1 : $pg; // This must be after ending correction (previous line) 
} 

if ($pgStart > 1) { // show 1 
    echo '<a href=page.php?page="1">1</a> ... '; 
} 

for($i = $pgStart; $i <= $tpages && $i < $pgStart + 5; $i++) { // show 5 pages 
    echo ' <a href=page.php?page="'.$i.'">'.$i.'</a> '; 
} 

if ($i < $tpages) { // show last 
    echo ' ... <a href=page.php?page="'.$tpages.'">'.$tpages.'</a>'; 
} 

編輯

這個腳本$_GET['page'] = 7$tpages = 20從PHP沙箱的輸出是(不換行):

<a href=page.php?page="1">1</a> ... 
<a href=page.php?page="5">5</a> 
<a href=page.php?page="6">6</a> 
<a href=page.php?page="7">7</a> 
<a href=page.php?page="8">8</a> 
<a href=page.php?page="9">9</a> 
... <a href=page.php?page="20">20</a> 
+0

感謝您答覆Mr.Jirka,它正在工作,但我沒有得到(...)之間的 –

+0

也是第一兩個沒有工作..像1和2沒有得到任何記錄。如果我點擊3然後我得到記錄 –

+0

我在沙箱中試過它,它應該工作。我只是忘記'''符合'for'循環。這三個點被硬編碼爲與第一個和最後一個頁面串在一起,並且沒有理由爲什麼前兩個記錄不起作用。我將從沙箱發佈輸出。 –