2013-05-16 28 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