2013-05-16 30 views
0

我想寫一個php簡單的分頁類。這裏是我的代碼:無法回顯php類變量

<?php 
class Pagination { 
public $rowsPerPage; 
public $currentPage; 
public $totalPages; 
public $totalRows; 
public $firstElement; 
public $lastElement; 


    public function paginate() 
     { 
      if(isset($_GET['page'])){ 
       $this->currentPage = $_GET['page'];  
      }   

         $this->totalPages = $this->totalRows/$this->rowsPerPage ; 
      return $this->totalPages; 

       $this->firstElement = $this->currentPage-1)*$this->rowsPerPage+1; 
       return $this->firstElement;     
     } 
} 

$totalRecords = 55; 
$paginator = new Pagination(); 
$paginator->totalRows = $totalRecords; 
$paginator->rowsPerPage = 5; 
$paginator->paginate(); 
echo $paginator->totalPages; 
echo $paginator->firstElement; 
?> 

我可以回顯$ paginator-> totalPages;但不是$ paginator-> firstElement。 我做錯了什麼? 謝謝

+0

我還_construct功能 公共函數_construct(){ \t \t \t $這 - > rowsPerPage = 5; $ this-> currentPage = 1; $ this-> totalPages = 0; \t \t \t $ this-> totalRows = 40; \t \t \t $ this-> firstElement = 0; \t \t \t $ this-> lastElement = 0; \t \t \t \t} – user2359958

+4

有超過一療法錯誤syntatic在$這個 - > firstElement你有一個右括號,但不是開放。 –

+0

打開[錯誤報告](http://php.net/error+reporting)。 –

回答

8

您的paginate函數有兩個return語句。

當代碼命中return $this->totalPages;時,它將停止運行該功能。因此,$this->firstElement = ...行永遠不會運行。

+2

打我吧! –

+2

@ChrisB:我是忍者^^ –

+0

哇..非常感謝:)) – user2359958