近期ecshop網站做活動,統計商品訂單量的時候沒有按商品名搜索的選項,只能手動查詢。這樣效率很低下,而且容易出錯。
現在為列表增加一個簡單的“按商品名搜索”表單項。效果如下圖

涉及到2個文件,分別是/admin/order.php,/admin/templates/order_list.htm,下面詳說。
第一步,增加表單項
在/admin/templates/order_list.htm第11行下面增加
1 |
商品名<input name="goods_name" type="text" id="goods_name" size="15"> |
這樣,代碼上下文基本為
1 |
{$lang.order_sn}<input name="order_sn" type="text" id="order_sn" size="15"> |
2 |
{$lang.consignee|escape}<input name="consignee" type="text" id="consignee"size="15"> |
3 |
商品名<input name="goods_name" type="text" id="goods_name" size="15"> |
5 |
<select name="status" id="status"> |
第二步,js獲取值
在/admin/order_list.htm第105行下面增加
1 |
listTable.filter['goods_name'] = Utils.trim(document.forms['searchForm'].elements['goods_name'].value); |
這樣,代碼上下文基本為
6 |
listTable.filter['order_sn'] = Utils.trim(document.forms['searchForm'].elements['order_sn'].value); |
7 |
listTable.filter['consignee'] = Utils.trim(document.forms['searchForm'].elements['consignee'].value); |
8 |
listTable.filter['goods_name'] = Utils.trim(document.forms['searchForm'].elements['goods_name'].value); |
9 |
listTable.filter['composite_status'] = document.forms['searchForm'].elements['status'].value; |
10 |
listTable.filter['page'] = 1; |
第三步,接收參數並處理
3.1)php文件接收參數並准備拼接sql
在/admin/order.php第4874行找到“$where = ‘WHERE 1 ‘;”,在下面增加
2 |
if ($filter['goods_name']) |
4 |
$join_goods = " LEFT JOIN " .$GLOBALS['ecs']->table('order_goods'). " AS og ON o.order_id=og.order_id LEFT JOIN " .$GLOBALS['ecs']->table('goods'). " AS g ON g.goods_id = og.goods_id "; |
5 |
$where .= " AND g.goods_name LIKE '%" . mysql_like_quote($filter['goods_name']) . "%'"; |
3.2)拼接sql
分別更改/admin/order.php第5025,5029,5041行sql語句為下面三句代碼
1,
1 |
$sql = "SELECT COUNT(*) FROM " . $GLOBALS['ecs']->table('order_info') . " AS o ,". |
2 |
$GLOBALS['ecs']->table('users') . " AS u ". $join_goods. $where; |
2.
1 |
$sql = "SELECT COUNT(*) FROM " . $GLOBALS['ecs']->table('order_info') . " AS o ".$join_goods. $where; |
3.
2 |
$sql = "SELECT o.order_id, o.order_sn, o.add_time, o.order_status, o.shipping_status, o.order_amount, o.money_paid," . |
3 |
"o.pay_status, o.consignee, o.address, o.email, o.tel, o.extension_code, o.extension_id, " . |
4 |
"(" . order_amount_field('o.') . ") AS total_fee, " . |
5 |
"IFNULL(u.user_name, '" .$GLOBALS['_LANG']['anonymous']. "') AS buyer ". |
6 |
" FROM " . $GLOBALS['ecs']->table('order_info') . " AS o " . |
7 |
" LEFT JOIN " .$GLOBALS['ecs']->table('users'). " AS u ON u.user_id=o.user_id ".$join_goods. $where . |
8 |
" ORDER BY $filter[sort_by] $filter[sort_order] ". |
9 |
" LIMIT " . ($filter['page'] - 1) * $filter['page_size'] . ",$filter[page_size]"; |
完成,這樣就可以了
有幾點可以改進:表單名可以存到系統語言包;可以按照此方法在“訂單查詢”那個大表單里擴展更多動能,緊急情況就先這樣…
為了搜索訂單更精准,決定完成之前說的改進:在“訂單查詢”那個大表單里擴展”商品名”檢索功能,相當簡單:
在/admin/templates/order_query.htm第七行下面增加
2 |
<td><div align="right"><strong>商品名</strong></div></td> |
3 |
<td colspan="3"><input name="goods_name" type="text" id="goods_name" size="40"></td> |
即可
這個過程中js自動獲取了表單的所有項目,而且之前我們已經完成了/admin/order.php中order_list()函數的改造