This a little pagination script that I am writing "Object Oriented" and I have no idea how to set the current page equal to total page, if the current page is greater. I also would really like it if anyone can tell me how to improve my code.
class pagination extends Db_connection {
public $per_page;
public $current_page;
public $sql;
//put your code here
function __construct($per = 3, $current = 1) {
parent::__construct();
$this->per_page = $per;
$this->current_page = $current;
}
function execute_query($sql) {
$this->sql = $sql;
$q = $this->query($sql);
return $q;
}
function total_pages() {
$total = ceil($this->count_rows() / $this->per_page);
return $total;
}
function current_page() {
if (isset($_GET['page']) && is_numeric($_GET['page']))
$this->current_page = intval($_GET['page']);
return $this->current_page;
}
function offset() {
$off = ($this->current_page() - 1) * $this->per_page;
return $off;
}
public function previous_page(){
//move to previous record by subtracting one into the current record
return $this->current_page - 1;
}
public function next_page(){
//mvove to next record by incrementing the current page by one
return $this->current_page + 1;
}
function count_rows() {
$nums = mysqli_fetch_row($this->execute_query("select count(post_id) from Post"));
return $nums[0];
}
function results() {
$off = $this->offset();
$per = $this->per_page;
$query = $this->execute_query("Select * from Post limit $off, $this->per_page");
while ($result = mysqli_fetch_array($query)) {
$data[] = $result;
}
return $data;
}
}