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;
}
}
-
\$\begingroup\$ The tiny amount of SQL you have in there looks fine, for all that it's worth. I don't know much about PHP but I'll try to tag someone who does. \$\endgroup\$Phrancis– Phrancis2014年06月17日 18:30:25 +00:00Commented Jun 17, 2014 at 18:30
1 Answer 1
If you have to identify the main objects here, we could conclude that you have at least 3
-- The connection to the DataBase
-- The pagination object that knows how to paginate a query
-- The query it self
so ie this is a possible solution:
abstract class DBConnection{
//Logic
public function executeQuery(Query $query)
{
$sql = $query->getSql();
//Logic and return results
}
}
class Query{
private $sql;
private $limit;
private $offset;
//getters and setters for each private property ... getSql ... setSql ...
public function __construct($sql)
{
$this->sql = $sql;
}
public function getSql()
{
return $this->sql += "offset $this->getOffset() limit $this->getLimit()";
}
}
class Paginator
{
private $currentPage;
private $perPage;
private $query;
private $connection;
//Getters and setters...
public function __construct(Query $query, $currentPage = 0, $perPage = 10)
{
$this->query = $query;
$this->connection = new DBConnection();
}
public function nextPage()
{
$this->currentPage++;
}
public function prevPage()
{
$this->currentPage--; //check if 0 not substract
}
public function execute()
{
$this->query->setOffset($this->currentPage * $this->perPage);
$this->query->setLimit($this->perPage);
return $this->connection->executeQuery($this->query);
}
}
-
\$\begingroup\$ Hi! It's helpful if rather than just dumping a lump of code that you post what changes you are making and why. The why is especially important here, as we are reviewing code more than writing it. \$\endgroup\$mdfst13– mdfst132015年12月26日 06:20:38 +00:00Commented Dec 26, 2015 at 6:20
Explore related questions
See similar questions with these tags.