近期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()函数的改造