php分页类第一次尝试
我正在尝试创建一个分页类,这是我第一次尝试创建一个真正的php类,除了那些基本的oop类教程,你知道的还有一些计算 我真正想要的是有人仔细看看,告诉我什么是好习惯,什么是坏习惯,一些我可以避免养成一些坏习惯:)。。也许还有一些我可以进一步发展的建议 课堂在上午看起来是这样的:php分页类第一次尝试,php,class,pagination,Php,Class,Pagination,我正在尝试创建一个分页类,这是我第一次尝试创建一个真正的php类,除了那些基本的oop类教程,你知道的还有一些计算 我真正想要的是有人仔细看看,告诉我什么是好习惯,什么是坏习惯,一些我可以避免养成一些坏习惯:)。。也许还有一些我可以进一步发展的建议 课堂在上午看起来是这样的: class Pagination { public $db = NULL; // Get the DB public $sqlQuery; // Get the sql string public
class Pagination {
public $db = NULL; // Get the DB
public $sqlQuery; // Get the sql string
public $per_page; // Get rows per page
public $total_results; // Get total results
public $total_pages;
public $p;
public $pages;
public $rows;
public $pagination;
public $url;
public function __construct($per_page,$sqlQuery,$db){
$this->per_page = $per_page;
$this->db = $db;
$this->sqlQuery = $sqlQuery;
$this->total_results = $this->db->query($this->sqlQuery)->num_rows;
$this->p = $p = NULL;
$this->url = 'http://'.$_SERVER['HTTP_HOST'] .$_SERVER['REQUEST_URI'];
// Makes sure we have a offset point
if(isset($_GET['p'])){
$this->p = $_GET['p'];
}else{
$this->p = 1;
}
// Get the amount of pages..
$this->total_pages = ceil($this->total_results/$this->per_page);
// Make sure we can't get false value, 0 or negative
if($this->p < 1){
$this->p = 1;
}
// Make sure property $p can't be higher than or max pages
if($this->p > $this->total_pages){
$this->p = $this->total_pages;
}
$limit = $this->p * $this->per_page;
$offset = $limit - $this->per_page;
$this->sqlQuery .= " limit ".$offset.",".$this->per_page; // possible error
$this->rows = $this->db->query($this->sqlQuery);
}
// Constructer ends
public function getResult(){
return $this->rows;
}
public function getPagination(){
$path = preg_replace("/&p=".$this->p."/","",$this->url);
$this->pagination = "";
if($this->p != 1){
$this->pagination .= "<a href='".$path."&p=1'>«</a> ";
$previous = $this->p-1;
$this->pagination .= "<a href='".$path."&p=".$previous."'>Forrige</a> ";
};
$range = 3;
//for($i=1;$i<=$this->total_pages;$i++){
for($i= ($this->p - $range); $i < (($this->p + $range) + 1); $i++){
if (($i > 0) && ($i <= $this->total_pages)) {
if($i == $this->p){
$this->pagination .= "<span>".$i."</span> ";
}else{
$this->pagination .= "<a href='".$path."&p=".$i."'>".$i."</a> ";
};
};
};
if($this->p != $this->total_pages){
$next = $this->p + 1;
$this->pagination .= "<a href='".$path."&p=".$next."'>Næste</a> ";
$this->pagination .= "<a href='".$path."&p=".$this->total_pages."'>»</a>";
};
return $this->pagination;
}
}
任何指针都会有帮助:)不必阅读整个代码,只需一个简单的问题:为什么所有类成员都声明为公共的
public
?我的主要意见是:这个类做得太多了。数据库交互、输出内容、分页计算。。。你真的应该在这里有多个解耦的类。这个问题似乎更适合你。你可以只查询一次SQL,并保留结果,而不需要再次查询,这样我就可以提取行并从类中删除db了?像这样的?
$db = db::getInstance();
$sqlQuery = "select * from movies order by name asc";
$result = new Pagination(5, $sqlQuery, $db);
$rows = $result->getResult();
$pagination = $result->getPagination();
$this->registry->template->rows = $rows;
$this->registry->template->pagination = $pagination;
$this->registry->template->show("movies_index");