我试图将opencart地址列表分解为每页20个,但它仍会显示第1页和第2页的所有列表。请查看代码,我必须做错事。
分页看起来很好
1 2> > |
显示1到20的31(2页)
address.php
中控制器帐户protected function getList()
页面上的代码(我也试过public function index()
)。
if (isset($this->request->get['page'])) {
$page = $this->request->get['page'];
} else {
$page = 1;
}
$data = array(
'start' => ($page - 1) * 20,
'limit' => 20
);
$address_total = $this->model_account_address->getTotalAddresses($data);
$results = $this->model_account_address->getAddresses();
$pagination = new Pagination();
$pagination->total = $address_total;
$pagination->page = $page;
$pagination->limit = 20;
$pagination->text = $this->language->get('text_pagination');
$pagination->url = $this->url->link('account/address', 'page={page}', 'SSL');
$this->data['pagination'] = $pagination->render();
和型号帐户address.php
上的代码
public function getDetails($data = array()) {
$sql = $this->db->query("SELECT * FROM `" . DB_PREFIX . "address` WHERE customer_id = '" . (int)$this->customer->getId() . "'");
if (isset($data['start']) || isset($data['limit'])) {
if ($data['start'] < 0) {
$data['start'] = 0;
}
if ($data['limit'] < 1) {
$data['limit'] = 20;
}
$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
}
$query = $this->db->query($sql);
$details = $query->rows;
return $details;
}
这是在address_list.tpl
<?php echo $pagination; ?>
请告知。
答案 0 :(得分:3)
问题出在您的模型中。在填写LIMIT
和OFFSET
之前运行查询,将模型更改为:
public function getDetails($data = array()) {
$sql = 'SELECT * FROM `' . DB_PREFIX . 'address` WHERE customer_id = ' . (int)$this->customer->getId();
if (isset($data['start']) || isset($data['limit'])) {
if ($data['start'] < 0) {
$data['start'] = 0;
}
if ($data['limit'] < 1) {
$data['limit'] = 20;
}
$sql .= ' LIMIT ' . (int)$data['start'] . ',' . (int)$data['limit'];
}
$query = $this->db->query($sql);
return $query->rows;
}
这应该这样做。
实际上在您第一次调用$sql = $this->db->query("...");
(模型方法的第一行)后,$sql
变量不包含SQL查询,而是包含属性的 OpenCart DB资源对象 row
(第一行),rows
(所有检索到的行)和num_rows
(检索到的行数)。因此,您的第二次调用$query = $this->db->query($sql);
应该会给您一个错误 - 如果没有显示,则可能隐藏了PHP错误。检查您的PHP错误日志(或OpenCart的错误日志)以查找。
修改强>:
然后在您的控制器中出现此逻辑错误:您要为LIMIT
设置OFFSET
和getTotalAddresses($data)
,20
应计算所有地址 - 但这将始终返回 getAddresses();
数字。另一方面,应该获得这些参数的方法不会得到它们:$address_total = $this->model_account_address->getTotalAddresses();
// no $data here as we want all the addresses to be counted ----^^
$results = $this->model_account_address->getAddresses($data);
// we pass $data here so the LIMIT is applied --------^^^^^
所以修复你的控制器来做到这一点:
getAddresses
还有一件事:在您的控制器中,您拨打 getDetails
,但在您的模型中 getDetails
已定义...您确定是否不想将getAddresses
重命名为 {{1}} ?