1. 程式人生 > >mycncart後臺列表篩選、分頁怎麼做

mycncart後臺列表篩選、分頁怎麼做

例如:

$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;

}

到此大功告成