2010-08-16 123 views
0

我寫了一個分頁類,它看起來像它不輸出正確數量的行。我有一張滿51條記錄的表格,而只顯示了30條記錄,而對於這些頁面,它只顯示了頁面1,它應該顯示頁面1和頁面2,頁面通過瀏覽器參數頁面= 1進行查看。當我嘗試查看頁面= 2時,我看到了其餘的記錄。這裏是班級。分頁類計算不正確

include_once("class.db.php"); 

     class Pagination { 

     var $param; 
     var $perPage; 
     var $adjacents; 
     var $start; 
     var $sql; 
     var $pageName; 


    function __construct() { 

     $this->db = new MysqlDB; 
     $this->db->connect(); 
    } 

     function setParam() { 

      if(isset($_GET['page']) && is_numeric($_GET['page']) && ($_GET['page'] > 0)) { 
       $this->param = $_GET['page']; 
      } else { 
       $this->param = 1; 
      } 
     } 


     function setIndex() { 
      $this->setParam(); 
      return $this->start = ($this->param * $this->perPage) - $this->perPage; 
     } 

     function showPagination() { 
      $qRows = $this->db->query($this->sql); 
      $numRows = $this->db->num_rows($qRows); 
      $numOfPages = ceil($numRows/$this->perPage); 
      $param = $this->param; 
      $pageName = $this->pageName; 



      print "<div class='pagination'>"; 

      print "<a href='$this->pageName?page=1' class='previous-off'> First </a>"; 


      // ----------------------------------------------------------------------------  
       // PRINT ALL PAGES TO THE LEFT // 
       if(($param - $this->adjacents) > 1) { 
        print "<span>...</span>"; 

        $lowerLimit = $param - $this->adjacents; 

        //print all on left side. 
        for($i = $lowerLimit; $i< $param; $i++) { 
         print "<a href='$pageName?page=$param = $i'> $i </a>"; 
        } 



        } else { 

          //print all numbers between current page and first page. 

          for($i = 1; $i < $param; $i++) { 
           print "<a href='$pageName?page=$i'> $i </a>"; 
          } 
         } 
      // ---------------------------------------------------------------------------- 



      //print current page 
      if(($param != 0) && ($param != $numOfPages)) { 
       print "<span class='current'>$param</span>"; 
      } 




      // ----------------------------------------------------------------------------   
         //PRINT ALL PAGES TO THE RIGHT 
        if(($param + $this->adjacents) < $numOfPages) { 

          $upperLimit = $param + $this->adjacents; 

          for($i=($param + 1); $i<=$upperLimit; $i++) { 
           print "<a href='$pageName?page=$i'> $i </a>"; 
          } 
          print "<span>...</span>"; 
         } else { 

          //print all page numbers if out of padded range 

          for($i = $param + 1; $i<$numOfPages; $i++) { 
           print "<a href='$pageName?page=$i'> $i </a>"; 
          } 

         } 
      // ---------------------------------------------------------------------------- 

      $lastPage = $numOfPages - 1; 
      print "<a class='next' href='$pageName?page=$lastPage'> Last </li>"; 

      print "</div>"; 
     } 





     function getData() { 
      $query = $this->sql; 
      $this->start = $this->setIndex(); 
      return "$query LIMIT $this->start, $this->perPage"; 
     } 

    } 

這是我如何使用類:

$ DB =新MySQLdb的; $ paginate = new分頁;

$paginate->pageName = "index.php"; //sets the page to use 
$paginate->perPage = 10; //show num of records per page 
$paginate->adjacents = 3; //current page adjacent to 
$paginate->sql = "select * from tbl_products"; //the main query 
$query = $db->query($paginate->getData()); 

while($row = mysql_fetch_object($query)) { 
print $row->pName."<br/>"; 
} 

$ paginate-> showPagination(); //顯示分頁格

+0

有誰知道嗎? – sam 2010-08-16 05:14:28

回答

0

在功能setIndex你必須寫:

return $this->start = (($this->param - 1) * $this->perPage) - $this->perPage; 

因爲查詢應該是這樣的:

第1頁:SELECT ... LIMIT 0,[nr_of_pages]

2頁:SELECT ... LIMIT [nr_of_pages]*1,[nr_of_pages] ...

+0

我做到了這一點,這就是查詢出了什麼錯誤。 select * from tbl_content where visible ='1'order by year desc LIMIT -30,30 – sam 2010-08-16 20:33:49

+0

可能$ this-> param變成零度 – Zsolti 2010-08-17 10:36:45