2013-12-10 211 views
-1

我在一個基於電子購物的大學項目上工作,我試過這段代碼來實現我的結果,這沒有錯誤,但當我按下一頁鏈接或「按鈕」時它不起作用,請幫助如果你訪問這個URL任何幫助,將不勝感激分頁問題

  <?php 
      $con = mysqli_connect("localhost","root","","php184_proj_db"); 

      // Check connection 
      if (mysqli_connect_errno($con)) 
       { 
       echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
       } 

      if (!(isset($pagenum))) 
      { 
      $pagenum = 1; 
      } 
      //Here we count the number of results 
      //Edit $qry to be your query 
      $qry = "SELECT * FROM posts"; 
      $result = mysqli_query($con,$qry); 
      $row = mysqli_num_rows($result); 
      //This is the number of results displayed per page 
      $page_rows = 5; 
      //This tells us the page number of our last page 
      $last = ceil($row/$page_rows); 
      //this makes sure the page number isn't below one, or more than our maximum pages 
      if ($pagenum < 1) 
      { 
      $pagenum = 1; //Pagination of MySQL Query Results Setting the Variables 
      } 
      elseif ($pagenum > $last) 
      { 
      $pagenum = $last; 
      } 
      //This sets the range to display in our query 
      $max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows; 
          $j=0; 
       $qry = "SELECT * FROM posts $max"; 
       $result = mysqli_query($con,$qry); 
       while($row = mysqli_fetch_array($result)) 
       { 
       $j++; 
        echo "<p>"; 
      // This shows the user what page they are on, and the total number Query and Results of pages 
      echo " --Page $pagenum of $last-- 
      <p>"; 
      // First we check if we are on page one. If we are then we don't need a 
      //link to the previous page or the first page so we do nothing. If we aren't 
      //then we generate links to the first page, and to the previous page. 

      if ($pagenum == 1) 
      { 
      } 
      else 
      { 
      echo " <a 
      href='{$_SERVER['PHP_SELF']}?pagenum=1'> 
      <<-First</a> 
      "; 
      echo " "; 
      $previous = $pagenum-1; 
      echo " <a 
      href='{$_SERVER['PHP_SELF']}? 
      pagenum=$previous'> <-Previous</a> 
      "; 
      } 
      //just a spacer 
      echo " ---- "; 
      //This does the same as above, only checking if we are on the last page, 
      //and then generating the Next and Last links 
      if ($pagenum == $last) 
      { 
      } 
      else { 
      $next = $pagenum+1; 
      echo " <a 
      href='{$_SERVER['PHP_SELF']}?pagenum=$next'>Next 
      -></a> "; 
      echo " "; 
      echo " <a 
      href='{$_SERVER['PHP_SELF']}?pagenum=$last'>Last 
      ->></a> "; 

      } 

      ?> 
+1

歡迎來到Stackoverflow。雖然你的問題值得關注,但我建議你看看這個[清單](http://meta.stackexchange.com/questions/156810/stack-overflow-question-checklist),看看你是否可以提供一些對您遇到的問題有更多見解/細節。 – kgdesouz

回答

3

http://example.com/somepage.php?key=val,你不會自動獲得一個變量$key PHP內。相反,您必須使用$_GET['key'],它將保留該值。 (在這種情況下:「VAL」)

所以,在你的代碼開頭的某處,添加以下內容:

if (isset($_GET['pagenum']) && $_GET['pagenum'] >= 1) { 
    $pagenum = (int) $_GET['pagenum']; 
} else { 
    $pagenum = 1; 
} 

這不僅創造$pagenum變量,並給它從URL中的價值,它也確保該值是一個有效的數字。如果沒有,或者URL不包含pagenum,則它設置爲1

可能的情景的:

  • pagenum不包含一個整數,而是一個字符串(甚至可能是一個SQL injection嘗試)
  • pagenum是負數,或者0
  • pagenum沒有被設置爲全部

在上述所有情況下,pagenum設置爲1

如果pagenum包含浮點數(例如1.5.),則該值將轉換爲整數。在1.5的情況下,pagenum將變爲1.

請記住,始終確保您清理用戶輸入。

+0

thx夥計,但它在任何情況下都不工作,我可以在地址欄中看到?page = 2,但它的追加下一個帖子? –

+0

對不起,我不確定你在這裏想說什麼。但是,如果您在鏈接中使用'?page = 2',則必須引用'$ _GET ['page']'而不是'$ _GET ['pagenum']''。您必須在URL中使用'$ _GET []'中的同一個鍵。 –