Page 1 of 1
Issue with php code
Posted: Wed Jan 06, 2010 10:48 am
by shanecody
I am getting the following error:
Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /home/radiosub/public_html/search_test/pagination.php on line 3
with this code:
Code: Select all
class Pagination {
public $current_page;
public $per_page;
public $total_count;
public function __construct($page=1, $per_page=20, $total_count=0){
$this->current_page = (int)$page;
$this->per_page = (int)$per_page;
$this->total_count = (int)$total_count;
}
}
There is more code, but for simplicity i am just adding where the issue is. I don't know what the error is, and the exact same code works on a different site. Any Ideas?
Re: Issue with php code
Posted: Wed Jan 06, 2010 11:11 am
by manohoo
Please post the entire code
Re: Issue with php code
Posted: Wed Jan 06, 2010 11:15 am
by shanecody
Code: Select all
class Pagination {
public $current_page;
public $per_page;
public $total_count;
public function __construct($page=1, $per_page=20, $total_count=0){
$this->current_page = (int)$page;
$this->per_page = (int)$per_page;
$this->total_count = (int)$total_count;
}
public function offset() {
// Assuming 20 items per page:
// page 1 has an offset of 0 (1-1) * 20
// page 2 has an offset of 20 (2-1) * 20
// in other words, page 2 starts with item 21
return ($this->current_page - 1) * $this->per_page;
}
public function total_pages() {
return ceil($this->total_count/$this->per_page);
}
public function previous_page() {
return $this->current_page - 1;
}
public function next_page() {
return $this->current_page + 1;
}
public function has_previous_page() {
return $this->previous_page() >= 1 ? true : false;
}
public function has_next_page() {
return $this->next_page() <= $this->total_pages() ? true : false;
}
}
Re: Issue with php code
Posted: Wed Jan 06, 2010 11:20 am
by manohoo
I duplicated your class in my system, then I added:
Code: Select all
$p=new Pagination;
echo "<pre>";
var_dump($p);
Output:
object(Pagination)#1 (3) {
["current_page"]=>
int(1)
["per_page"]=>
int(20)
["total_count"]=>
int(0)
}
... no errors
Re: Issue with php code
Posted: Wed Jan 06, 2010 11:23 am
by AbraCadaver
Is it possible that you are using PHP 4 shanecody?
Re: Issue with php code
Posted: Wed Jan 06, 2010 11:24 am
by manohoo
Code: Select all
$p=new Pagination(10,20,30);
echo "<pre>";
var_dump($p);
Output:
object(Pagination)#1 (3) {
["current_page"]=>
int(10)
["per_page"]=>
int(20)
["total_count"]=>
int(30)
}
Re: Issue with php code
Posted: Wed Jan 06, 2010 11:42 am
by shanecody
PHP version 5.2.11
It has to be something with the server if the code works for you.
do you know of any settings that would cause this?
Re: Issue with php code
Posted: Wed Jan 06, 2010 12:20 pm
by AbraCadaver
shanecody wrote:PHP version 5.2.11
It has to be something with the server if the code works for you.
do you know of any settings that would cause this?
Re: Issue with php code
Posted: Wed Jan 06, 2010 12:26 pm
by shanecody
PHP 4.4.9
Re: Issue with php code
Posted: Wed Jan 06, 2010 12:31 pm
by AbraCadaver
shanecody wrote:PHP 4.4.9
That was my point in the previous post. public, private and protected keywords aren't available in PHP 4, neither is __construct().
Re: Issue with php code
Posted: Wed Jan 06, 2010 12:39 pm
by AbraCadaver
The best bet is to upgrade to PHP 5. If not and this is all the code you have, then remove 'public' from the function declarations, change 'public' to 'var' for the properties and change __construct to Pagination
Re: Issue with php code
Posted: Wed Jan 06, 2010 12:52 pm
by shanecody
Thank you!