在Ecmall的二次開發中,分頁是必不可少的。這個系統已經自帶了分頁功能,下面來看看如何使用這個分頁。
下面是一個自定義的類,用於查看訂單的詳細情況。關鍵在於get_order_data()這個方法,分頁的使用也在這個方法的內部了。應該有的注釋都有了,應該會比較容易懂,我不就多說了。
<?php define('NUM_PER_PAGE', 15); // 每頁顯示數量 class NowaMagicApp extends MallbaseApp { public function index() { /* 分頁信息 */ $page = $this->_get_page(NUM_PER_PAGE); $page['item_count'] = $stats['total_count']; $this->_format_page($page); $this->assign('page_info', $page); $this->display('gorder.index.html'); } /* 訂單記錄 */ function orderslog() { $goods_id = empty($_GET['id']) ? 0 : intval($_GET['id']); if (!$goods_id) { $this->show_warning('Hacking Attempt'); return; } $data = $this -> get_order_data($goods_id); if ($data === false) { return; } $this->assign('order', $data); $this->display('gorder.index.html'); } function get_order_data($goods_id) { //clean_cache(); $cache_server =& cache_server(); //print_r($cache_server); $key = 'order_' . $goods_id; //$key = $this->_get_cache_id(); $r = $cache_server->get($key); $cached = true; $db = &db(); $sql = "select count(*) from shop_order a, shop_order_extm b, shop_order_goods c where a.order_id = b.order_id and b.order_id = c.order_id and c.goods_id = '".$goods_id."' and a.status != '11' and a.status != '0' and a.status != '20' order by a.add_time desc "; //echo $sql; $num = $db -> getone($sql); //求出總記錄數 $page = $this->_get_page(NUM_PER_PAGE); //每頁顯示的條數,默認是10條 $page['item_count'] = $num; // 返回一個數組$page,$page['limit']=0,10 $this->_format_page($page); //格式化分頁 $sql2 = "select a.order_id, a.buyer_name, a.add_time, a.status, b.phone_tel, b.phone_mob, b.consignee, c.price, c.quantity, c.goods_id from shop_order a, shop_order_extm b, shop_order_goods c where a.order_id = b.order_id and b.order_id = c.order_id and c.goods_id = '".$goods_id."' and a.status != '11' and a.status != '0' and a.status != '20' order by a.add_time desc limit ".$page['limit']; $result = $db -> query($sql2); $this -> assign('page_info',$page); //向模板頁傳遞頁數 $this -> assign('que',$sql2); //向模板頁傳遞查詢結果 //$r = array(); while($myrow = $db -> fetch_array($result)) { $r[] = $myrow; } $cache_server->set($key, $r, 1); return $r; } } ?>
簡化如下:
Define("LIMIT",10); $goods_mod = & db('test');//構建實體模型(操作表) $count = 'select count(id) from test'; $num = $goods_mod -> getone($count);//求出總記錄數 $page = $this->_get_page(LIMIT);//每頁顯示的條數,默認是10條 $page['item_count'] = $num;// 返回一個數組$page,$page['limit']=0,10 $this->_format_page($page);//格式化分頁 $sql = 'select id,title,content from test order by id desc limit '.$page['limit']; $que = $goods_mod -> getAll($sql);//查詢記錄 $this -> assign('page_info',$page); //向模板頁傳遞頁數 $this -> assign('que',$que); //向模板頁傳遞查詢結果