PHP php 应用程序中的分页
PHP pagination in php application
我使用 PHP-MVC (https://github.com/panique/mini) for small projects, mostly just for structure as I like the design pattern but I'm struggling to implement a pagination class I always use into it. I use the following: https://daveismyname.com/pagination-with-a-php-class-bp#.VNzLzZ2sWWA
我通过 application.php 加载 class 基本上是一个 bootstrap 文件来加载 class 和其他一些文件。在我的模型中,我有以下内容:
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);
?>
现在困扰我的部分是模型中的这个部分,我不确定该怎么做:
echo $pages->page_links();
显然我无论如何都不应该直接从模型中回显输出,但我不确定将其实现到视图中的最佳方式?
没关系我现在想通了,我只是return一个像这样的多维数组:
return array('pages' => $pages, 'data' => $data);
然后在控制器中选取这些数组并将它们分成两个传递给视图的变量:
$arrays = $albums_model->pagi();
$albums = $arrays['data'];
$pages = $arrays['pages'];
然后在需要时简单地循环或回显。
我使用 PHP-MVC (https://github.com/panique/mini) for small projects, mostly just for structure as I like the design pattern but I'm struggling to implement a pagination class I always use into it. I use the following: https://daveismyname.com/pagination-with-a-php-class-bp#.VNzLzZ2sWWA
我通过 application.php 加载 class 基本上是一个 bootstrap 文件来加载 class 和其他一些文件。在我的模型中,我有以下内容:
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);
?>
现在困扰我的部分是模型中的这个部分,我不确定该怎么做:
echo $pages->page_links();
显然我无论如何都不应该直接从模型中回显输出,但我不确定将其实现到视图中的最佳方式?
没关系我现在想通了,我只是return一个像这样的多维数组:
return array('pages' => $pages, 'data' => $data);
然后在控制器中选取这些数组并将它们分成两个传递给视图的变量:
$arrays = $albums_model->pagi();
$albums = $arrays['data'];
$pages = $arrays['pages'];
然后在需要时简单地循环或回显。