Hey, I have been working on a class to use for pagination and I ran into a problem. I have a function that sets the variables that I will need, then I access them in the other funtions. For some reason one of my functions can't access any of the variables from the class, but the other functions can still access the variables.
Here is the code:
File: pagination.inc.php
<?php
class Pagination{
function setUp($num = '',$numRows,$pageNum = ''){
// The number of rows per page
if($num != ''){
$this->num = $num;
}else{
$this->num = 25;
}
// Get the number of rows in the table
$this->numRows = $numRows;
// Divide number of rows, by number per page and round up to get the number of pages we need
$this->numPages = ceil($this->numRows / $this->num);
if($pageNum != ''){ // If the page is set it will be that page.
if($pageNum > $this->numPages){
$this->pageNum = $this->numPages;
}elseif($pageNum < 1){
$this->pageNum = '1';
}else{
$this->pageNum = $pageNum;
}
}else{ // else it will be the first page
$this->pageNum = '1';
}
// The number of rows already shown, will be used for LIMIT in query
$this->shown = ($this->pageNum - 1) * $this->num;
}
function pageNav(){
$prev = $this->pageNum-1;
$next = $this->pageNum+1;
$this->pageList = "Pages: ";
if($this->pageNum != '1'){
$this->pageList .= "<a href='?pageNum=1'>First</a> | ";
$this->pageList .= "<a href='?pageNum=$prev'>Prev</a> | ";
}else{
$this->pageList .= "First | ";
$this->pageList .= "Prev | ";
}
for($i = 1; $i <=$this->numPages; $i++){
$this->pageList .= "<a href='?pageNum=$i'>$i</a> | ";
}
if($this->pageNum != $this->numPages){
$this->pageList .= "<a href='?pageNum=$next'>Next</a> | ";
$this->pageList .= "<a href='?pageNum=$this->numPages'>Last</a>";
}else{
$this->pageList .= "Next | ";
$this->pageList .= "Last";
}
return $this->pageList;
}
function limitStatement(){
return "LIMIT $this->shown,$this->num";
}
}
?>
The limitStatement() function is the one that cannot access any variables. Here are the errors I get:
Notice: Undefined property: Pagination::$shown in pagination.inc.php on line 55
Notice: Undefined property: Pagination::$num in pagination.inc.php on line 55
I have even tried defining them as globals within the function, and that doesn't work either. I tried to define them as variables in the beginning of the class, and that only worked when I assigned a value to the variables.
In the past I have had problems with using reserved words for variables, so I changed the function name to see if that was it, and it did not fix the problem.
Any help that you can give me would be useful.
Thank you,
Key