例如:
$row=$this->model_healthdata_health_data->gethealthdata($post);
$data["healthdata"]=$row;
$row是传到view的列表数组
那么要实现分页首先view页添加:
<div class="row">
<div class="col-sm-6 text-left"><?php echo $pagination; ?></div>
<div class="col-sm-6 text-right"><?php echo $results; ?></div>
</div>
位置自己对照其他模版页查看
接着controller文件中添加:
if (isset($this->request->get['page'])) {
$page = $this->request->get['page'];
} else {
$page = 1;
}
$post=array(
"user_id"=>$user_id,
'start' => ($page - 1) * $this->config->get('config_limit_admin'),
'limit' => $this->config->get('config_limit_admin')
);
$total = $this->model_healthdata_health_data->getTotal($post);//传递所有筛选条件和分页信息
$pagination = new Pagination();
$pagination->total = $total;//总页数
$pagination->page = $page;//地址栏获取的当前页码
$pagination->limit = $this->config->get('config_limit_admin');每页多少,可以在后台商店设置的选项里面修改
$pagination->url = $this->url->link('healthdata/health_data', 'token=' . $this->session->data['token']. '&page={page}', 'SSL');
$data['pagination'] = $pagination->render();
$data['results'] = sprintf($this->language->get('text_pagination'), ($total) ? (($page - 1) * $this->config->get('config_limit_admin')) + 1 : 0, ((($page - 1) * $this->config->get('config_limit_admin')) > ($total - $this->config->get('config_limit_admin'))) ? $total : ((($page - 1) * $this->config->get('config_limit_admin')) + $this->config->get('config_limit_admin')), $total, ceil($total / $this->config->get('config_limit_admin')));
接着model中:
public function getTotal($post){//统计总页数
$where=null;
if($post["user_id"]){
$where .=" and h.user_id=".$post["user_id"];
}//筛选条件加入
$sql = "select count(*) as count from " . DB_PREFIX . "表名 ," . DB_PREFIX . "customer c where c.customer_id=h.user_id ".$where;
$query = $this->db->query($sql);
$data=$query->row;
return $data['count'];
}
public function gethealthdata($post){//返回列表
$where=null;
if($post["user_id"]){
$where .=" and h.user_id=".$post["user_id"];
}
if (isset($post['start']) || isset($post['limit'])) {
if ($post['start'] < 0) {
$post['start'] = 0;
}
if ($post['limit'] < 1) {
$post['limit'] = 20;
}
$where .= " LIMIT " . (int)$post['start'] . "," . (int)$post['limit'];
}
$sql = "select * from" . DB_PREFIX . "health_data h," . DB_PREFIX . "customer c where c.customer_id=h.user_id ".$where;
$query = $this->db->query($sql);
return $query->rows;
}
到此大功告成