Issue with php code

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
shanecody
Forum Newbie
Posts: 23
Joined: Fri May 08, 2009 4:12 pm

Issue with php code

Post 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?
User avatar
manohoo
Forum Contributor
Posts: 201
Joined: Wed Dec 23, 2009 12:28 pm

Re: Issue with php code

Post by manohoo »

Please post the entire code
shanecody
Forum Newbie
Posts: 23
Joined: Fri May 08, 2009 4:12 pm

Re: Issue with php code

Post 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;
  }
}
User avatar
manohoo
Forum Contributor
Posts: 201
Joined: Wed Dec 23, 2009 12:28 pm

Re: Issue with php code

Post 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
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Issue with php code

Post by AbraCadaver »

Is it possible that you are using PHP 4 shanecody?
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
manohoo
Forum Contributor
Posts: 201
Joined: Wed Dec 23, 2009 12:28 pm

Re: Issue with php code

Post 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)
}
shanecody
Forum Newbie
Posts: 23
Joined: Fri May 08, 2009 4:12 pm

Re: Issue with php code

Post 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?
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Issue with php code

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

Code: Select all

echo PHP_VERSION;
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
shanecody
Forum Newbie
Posts: 23
Joined: Fri May 08, 2009 4:12 pm

Re: Issue with php code

Post by shanecody »

PHP 4.4.9
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Issue with php code

Post 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().
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Issue with php code

Post 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
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
shanecody
Forum Newbie
Posts: 23
Joined: Fri May 08, 2009 4:12 pm

Re: Issue with php code

Post by shanecody »

Thank you!
Post Reply