PHP应用程序中的PHP分页
我将PHP-MVC用于小型项目,主要是用于结构,因为我喜欢设计模式,但我正在努力实现一个我一直在使用的分页类。我使用以下方法: 我通过application.php加载这个类,基本上是一个引导文件来加载这个类和其他一些类。在我的模型中,我有以下内容:PHP应用程序中的PHP分页,php,pagination,Php,Pagination,我将PHP-MVC用于小型项目,主要是用于结构,因为我喜欢设计模式,但我正在努力实现一个我一直在使用的分页类。我使用以下方法: 我通过application.php加载这个类,基本上是一个引导文件来加载这个类和其他一些类。在我的模型中,我有以下内容: public function pagi() { $pages = new Paginator('30','p'); //get number of total records $sql = 'SELECT co
public function pagi()
{
$pages = new Paginator('30','p');
//get number of total records
$sql = 'SELECT count(id) FROM albums';
$query = $this->db->prepare($sql);
$query->execute();
$row = $query->fetch(PDO::FETCH_NUM);
$total = $row[0];
//pass number of records to
$pages->set_total($total);
$sql = 'SELECT * FROM albums '.$pages->get_limit();
$query = $this->db->prepare($sql);
$query->execute();
$data = $query->fetchAll();
//create the page links
echo $pages->page_links();
return $data;
}
$albums_model = $this->loadModel('AlbumsModel');
$albums = $albums_model->pagi();
在我的控制器中,我调用以下命令:
public function pagi()
{
$pages = new Paginator('30','p');
//get number of total records
$sql = 'SELECT count(id) FROM albums';
$query = $this->db->prepare($sql);
$query->execute();
$row = $query->fetch(PDO::FETCH_NUM);
$total = $row[0];
//pass number of records to
$pages->set_total($total);
$sql = 'SELECT * FROM albums '.$pages->get_limit();
$query = $this->db->prepare($sql);
$query->execute();
$data = $query->fetchAll();
//create the page links
echo $pages->page_links();
return $data;
}
$albums_model = $this->loadModel('AlbumsModel');
$albums = $albums_model->pagi();
在我看来,出于调试目的,我只是简单地循环了一下:
<?php
print_r($albums);
?>
显然,我不应该直接从模型回显输出,但我不确定在视图中实现这一点的最佳方法?没关系,我现在已经明白了,我只是返回一个多维数组,如下所示:
return array('pages' => $pages, 'data' => $data);
然后在控制器中拾取这些数组,并将它们分离为两个变量,并将其传递给视图:
$arrays = $albums_model->pagi();
$albums = $arrays['data'];
$pages = $arrays['pages'];
然后在需要时简单地循环或回音