0

I want to write a php simple pagination class. Here is my code:

<?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;  
?>

I can echo $paginator->totalPages; but not $paginator->firstElement. What I do wrong ? Thanks

3
  • I have also _construct function public function _construct() { $this->rowsPerPage = 5; $this->currentPage = 1; $this->totalPages = 0; $this->totalRows = 40; $this->firstElement = 0; $this->lastElement = 0; } Commented May 16, 2013 at 21:42
  • 4
    There is a syntatic error over ther in $this->firstElement you have a closing parenthesis, but not a opening. Commented May 16, 2013 at 21:43
  • Turn on error reporting. Commented May 16, 2013 at 21:50

1 Answer 1

8

Your paginate function has two return statements.

When the code hits the return $this->totalPages;, it will stop running the function. Therefore, the $this->firstElement = ... line is never ran.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.