2015-09-05 57 views
1

我有標準的分頁,效果很好。我想要做的就是顛倒。所以導航將從最後開始。 5 | 4 | 3 | 2 | 1。我每頁的限制是15,所以我想第1頁第1頁包含15個元素,最後一頁將包含最新的元素。我知道這是非常不尋常的方法,但這是我想要做的。但是我需要有人把我放在正確的道路上,請幫助一下。謝謝。PHP分頁倒掛

我funcional代碼在這裏:(縮短)

<? 

$adjacents = 4; 

$result = $mysqli->query("SELECT * FROM table"); 
$total_pages = $result->num_rows; 
$result->close(); 

    /* Setup vars for query. */ 
    $targetpage = "articles"; 
    $limit = 15;         
    $page = $_GET['page']; 
    if($page) 
     $start = ($page - 1) * $limit; 
    else 
     $start = 0;   

    /* Get data. */ 
    $sql = $mysqli->query("SELECT * FROM table ORDER BY id DESC LIMIT $start, $limit"); 


    /* 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 


while($row = $sql->fetch_array()) { 

$name = $row['name']; 
$ource = $row['source']; 

} 

?> 

<?=$pagination?> 
<? $sql->close(); ?> 
+0

有什麼問題嗎?你不能只給3頁的代碼,並要求某人爲你解決它。開始自己開展工作,如果遇到一些特定問題,請尋求幫助。 – MilanG

+0

Hi MilanG - 我不太清楚如何開始?而且,我並不期待任何人爲我解決這個問題..但是有人可以用一條線將我放在正確的道路上,爲我節省很多時間,或者向我展示我看不到的邏輯。 – Nita

回答

0
<?php 
$result = $mysqli->query("SELECT id FROM table"); 
$total_results = $result->num_rows; 
$result->close(); 

    /* Setup vars for query. */ 

    $targetpage = "moving-articles"; 
    $limit = 5; 
    $adjacents = 5; 
    $lastpage = ceil($total_results/$limit);         
    $page = $_GET['page']; 
    if($page) 
     $start = ($page - 1) * $limit; 
    else 
     $start = ($lastpage - 1) * $limit;; 

    /* Get data. */ 
    $sql = $mysqli->query("(SELECT * FROM table ORDER BY datestamp LIMIT $start, $limit) ORDER BY datestamp DESC"); 
    if ($page == '') $page = $lastpage; //if no page var is given, default to last page (as first page) 
    $prev = $page - 1;       //previous page is page - 1 
    $next = $page + 1;       //next page is page + 1 
    $lpm1 = $lastpage - 1;       


    /* Draw the pagination object. */ 

    $pagination = ""; 
    if($lastpage > 1) 
    { 
     $pagination .= " 
     <div class='pagination'>"; 
     /* Next Button */ 
     if ($page < $lastpage) 
      $pagination.= "<a href='$targetpage/page/$next/'>« next</a>"; 
     else 
      $pagination.= "<span class='disabled'>« next</span>"; 

     if ($lastpage < $adjacents * 4) //not enough pages to bother breaking it up 
     { 
     /* Backwards Pagination */ 
     for ($counter = $lastpage; $counter > 0; $counter--) { 
       if ($counter == $page) 
        $pagination.= "<span class='current'>$counter</span>"; 
       else 
        $pagination.= "<a href='$targetpage/page/$counter/'>$counter</a>"; 
     } 
     } 

     else if($lastpage > $adjacents * 4) //enough pages to hide some 
     { 
      //close to beginning; only hide later pages 
      if($page > $lastpage - $adjacents * 2)  
      { 

       for ($counter = $lastpage ; $counter > abs(($adjacents * 2) - $lastpage) - 1; $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/2/'>2</a>"; 
       $pagination.= "<a href='$targetpage/page/1/'>1</a>"; 

      } 
     //in middle; hide some front and some back 
      else if($page <= $lastpage - $adjacents * 2 && $page > $adjacents * 2) 
      { 

       $pagination.= "<a href='$targetpage/page/$lastpage/'>$lastpage</a>";  
       $pagination.= "<a href='$targetpage/page/$lpm1/'>$lpm1</a>"; 
       $pagination.= "..."; 
       for ($counter = $page + $adjacents; $counter > $page - $adjacents - 1 ; $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/2/'>2</a>"; 
       $pagination.= "<a href='$targetpage/page/1/'>1</a>"; 

      } 
      //close to end; only hide early pages 
      else 
      { 
       $pagination.= "<a href='$targetpage/page/$lastpage/'>$lastpage</a>";  
       $pagination.= "<a href='$targetpage/page/$lpm1/'>$lpm1</a>"; 
       $pagination.= "..."; 
       for ($counter = ($adjacents * 2) +1 ; $counter >= 1; $counter--) 
       { 
        if ($counter == $page) 
         $pagination.= "<span class='current'>$counter</span>"; 
        else 
         $pagination.= "<a href='$targetpage/page/$counter/'>$counter</a>";     
       } 
      } 

     } 


     /* Previous Button */ 
     if ($page > $counter - 1) 
      $pagination.= "<a href='$targetpage/page/$prev/'>previous »</a>"; 
     else 
      $pagination.= "<span class='disabled'>previous »</span>"; 

     $pagination.= "</div>";  
    } 

?> 
+0

如果對這段代碼有任何意見或建議,請告訴我 – Nita

2

嗯......第一個技巧 - 不要使用簡短的開幕PHP標籤。他們已被棄用,並可以給你一些服務器上的問題..啓動PHP代碼<?php。其次,在您的代碼中,您將混合部分混合顯示傳呼機和頁面內容。 mySql查詢用於獲取當前頁面的行。但對於尋呼機,您將需要類似的查詢,這將只返回所有行的計數(因此沒有限制部分)。

$sql = $mysqli->query("SELECT COUNT(*) FROM table"); 

類似的東西...

當你拿到計數次數(假設你有45行總計)。你必須計算總頁數。這將是:

$pages_number = ceil ($rows_count/$limit); 

在我們的案例中,我們將有3(頁)。

然後,你需要一些for循環會落後,從頁面數爲1,是這樣的:

for ($i = $pages_number; $i > 1; $i--){ 
// write out current link 
} 

這個循環會給你數3,2,1 ......這樣創建鏈接將它們打印出來並打印出來。

+0

是的,非常感謝MilanG,這是一個好開始! – Nita

0
<?php 
$result = $mysqli->query("SELECT COUNT(*) FROM webelements WHERE web='ALR' AND cat='Article'"); 
$total_results = $result->num_rows; 
$result->close(); 

    /* Setup vars for query. */ 
    $targetpage = "moving-articles"; 
    $limit = 15;         
    $page = $_GET['page']; 
    if($page) 
     $start = ($page - 1) * $limit; 
    else 
     $start = 0;   

    /* Get data. */ 
    $sql = $mysqli->query("SELECT * FROM webelements WHERE web='ALR' AND cat='Article' ORDER BY datestamp DESC LIMIT $start, $limit"); 

    $prev = $page - 1;       //previous page is page - 1 
    $next = $page + 1;       //next page is page + 1 
    $lastpage = ceil($total_results/$limit); //lastpage = total results/items per page, rounded up. 
    if ($page == 0) $page = $lastpage;   //if no page var is given, default to last page (as first page) 
    $lpm1 = $lastpage - 1;      //last page minus 1 

    /* Draw the pagination object. */ 

    $pagination = ""; 
    if($lastpage > 1) 
    { 
     $pagination .= " 
     <div class='pagination'>"; 
     /* Next Button */ 
     if ($page < $lastpage) 
      $pagination.= "<a href='$targetpage/page/$next/'>« next</a>"; 
     else 
      $pagination.= "<span class='disabled'>« next</span>"; 

     /* Backwards Pagination */ 
     for ($counter = $lastpage; $counter > 0; $counter--) { 
       if ($counter == $page) 
        $pagination.= "<span class='current'>$counter</span>"; 
       else 
        $pagination.= "<a href='$targetpage/page/$counter/'>$counter</a>"; 
     } 

     /* Previous Button */ 
     if ($page > $counter - 1) 
      $pagination.= "<a href='$targetpage/page/$prev/'>previous »</a>"; 
     else 
      $pagination.= "<span class='disabled'>previous »</span>"; 

     $pagination.= "</div>";  
    } 
?> 
+0

你對這個有任何意見@MilanG? – Nita

+0

關於第一個查詢 - 您不必獲取所有行並對它們進行計數....使用mysql計數函數 - 速度更快。如果代碼的其餘部分起作用,那就沒問題了。 :) – MilanG

+0

@MilanG,我設法向後顯示導航,但相應的記錄不正確。我認爲這是有價值的$ start和$ limit值。我做錯了什麼? – Nita