总结ecshop二次开发50个小技巧

2016-07-07 14:55 来源:www.chinab4c.com 作者:ecshop专家



最近很忙,也没时间去打理一些资料和开发笔记,去年总结了使用ecshop电子商务系统的100个小问题,被ecshop爱好的朋友大量转载,今年应更多朋友的邀请。总结50条ecshop二次开发小技巧。希望对大家有所帮助。我将尽力快速的完成完善本文.但是有一点,不会说的太详细,也不会有完整的代码。只提供思路给大家.



1:如何统计ecshop商品在订单中出现的次数

回答count = $db -> getOne("select sum(goods_number) from ".$ecs->table('order_goods')." where goods_id = '$goods[goods_id]'");



2:ecshop详细页面如何通过onclick点击事件来控制购买,特别用在批发,批发存在一个最小购买量控制.

回答:

<script>
function adgtz(){
var number = document.getElementById("ordernumber").value;
var min_number = {$goods.min_number};
var goods_num = 1000;
if(number > goods_num - min_number){
alert("最多能买"+goods_number+"件");
}
number=parseInt(number)+min_number;
document.getElementById("ordernumber").value = number;
}
function adgtzmm(){
var number = document.getElementById("ordernumber").value;
var min_number = {$goods.min_number};
if (number>min_number){
number=parseInt(number)-min_number;
}
document.getElementById("ordernumber").value = number;

}
</script>

3:如何用js去验证手机号码.

回答:var reg =/^[1]([3][0-9]{1}|50|53|59|58|56|86|80|89|87|88|85|86|55|51|52|57)[0-9]{8}$/;

4:ecshop中如何取得每个月的1号时间?

回答:local_mktime(0,0,0,date("m"),0,date("Y"));使用ecshop的local_mktime函数.

5:ecshop如何直接关闭窗口而不弹出提示

回答:<a href="javascript:window.opener=null;window.close();">

6:如何统计ecshop某商品下被审核过的评论数量。

回答:$ccount = $db -> getOne("select count(*) cc from ".$ecs->table('comment')." where id_value = '$goods_id' and comment_type = '0' and status = '1' and parent_id = '0'");

有关ecshop评论,请参考本文

7:ecshop中js如何将时间格式互转

回答:date1 = new Date();
var b = Date.UTC(date1.getYear(),date1.getMonth()+1,date1.getDate(),0,0,0)
time1 = frm.elements['best_time'].value;

y = time1.substring(0,4);
m = time1.substring(5,7);
d = time1.substring(8,10);
var b2 = Date.UTC(y,m,d,0,0,0)

8:如何解析ecshop flash的xml

回答:

$str = @file_get_contents("data/flash_data.xml");
if($str){
preg_match_all("/item_url=\"(.*?)\"/",$str,$url);
preg_match_all("/text=\"(.*?)\"/",$str,$text);
preg_match_all("/link=\"(.*?)\"/",$str,$link);
$ar_flash = array();
if(sizeof($url[1])){
for($i=0;$i<sizeof($url[1]);$i++){
$ar_flash[$i]['url']= $url[1][$i];
$ar_flash[$i]['text'] = $text[1][$i];
$ar_flash[$i]['link'] = $link[1][$i];

}
$smarty->assign('ar_flash',$ar_flash);
}

}
9:ecshop中如何正则提取中文

回答:preg_match("/[".chr(0xa1)."-".chr(0xff)."]+/",ecs_iconv("UTF-8","GBK",$v['brand_name']),$cn);

10:ecshop如何取得订单中所属区域

$p = $GLOBALS['db']->getOne("select region_name from ".$GLOBALS['ecs']->table('region')." where region_id = '".$re1['province']."'");
$c = $GLOBALS['db']->getOne("select region_name from ".$GLOBALS['ecs']->table('region')." where region_id = '".$re1['city']."'");
$d = $GLOBALS['db']->getOne("select region_name from ".$GLOBALS['ecs']->table('region')." where region_id = '".$re1['district']."'");

11:ecshop中如何分别提取中文和英文

回答:utf下面

$pregstr = "/[\x{4e00}-\x{9fa5}]+/u";
preg_match($pregstr,$v['brand_name'],$ch);

gbk下面

$strtest = “yyg中文字符yyg”;

$pregstr = "/([".chr(0xb0)."-".chr(0xf7)."][".chr(0xa1)."-".chr(0xfe)."])+/i";

if(preg_match($pregstr,$strtest,$matchArray)){

echo $matchArray[0];

}

12:如何控制ecshop商品[/url]总价格只有ecshop会员特殊等级才能查看

回答:ecshop的goods.php

$is_spe = $GLOBALS['db'] -> getOne("select special_rank from ".$GLOBALS['ecs']->table('user_rank')." where rank_id = '".$_SESSION['user_rank']."'");
if($is_spe){
$res['result'] = price_format($shop_price * $number);
}else{
$string = "<a href=article-21-%E6%80%8E%E4%B9%88%E6%B3%A8%E5%86%8C.html>请联系客服</a>";
$res['result'] = $string;
}

13:调试ecshop程序出现以下错误Fatal error: Cannot redeclare是什么意思?

回答:Fatal error: Cannot redeclare,意思是你运行的程序中,某函数重新申明了两次,要删除其中一个同名函数.


回答:
14:ecshop如何判断是否当天登陆,并赠送积分.
回答:/* 赠送积分 */
$day &#61; local_mktime&#40;0,0,0,date&#40;"m"),date&#40;"d"),date&#40;"Y"));
$last_time &#61; $db -&#62; getOne&#40;"select last_login from ".$ecs-&#62;table&#40;'users')." where user_name &#61; '".$username."'");
if&#40;$last_time &#60; $day){
log_account_change&#40;$_&#83;E&#83;&#83;ION['user_id'], 0, 0, 0, 10, '每天登陆送积分', ACT_OTHER);
}

15:如何在ecshop订单列表中列表出订单中所包含的商品编号.
回答:
function get_user_orders&#40;$user_id, $num &#61; 10, $start &#61; 0)
{
/* 取得订单列表 */
$arr &#61; array&#40;);
global $ecs;
global$db;
$sql &#61; "&#83;ELECT order_id, order_sn, order_status,consignee, shipping_status, pay_status, add_time, " .
"&#40;goods_amount + shipping_fee + insure_fee + pay_fee + pack_fee + card_fee + tax - discount) A&#83; total_fee ".
" FROM " .$GLOBAL&#83;['ecs']-&#62;table&#40;'order_info') .
" WHERE user_id &#61; '$user_id' ORDER BY add_time DE&#83;C";
$res &#61; $GLOBAL&#83;['db']-&#62;&#83;electLimit&#40;$sql, $num, $start);
while &#40;$row &#61; $GLOBAL&#83;['db']-&#62;fetchRow&#40;$res))
{
if &#40;$row['order_status'] &#61;&#61; O&#83;_UNCONFIRMED)
{
$row['handler'] &#61; "&#60;a href&#61;\"user.php?act&#61;cancel_order&order_id&#61;" .$row['order_id']. "\" onclick&#61;\"if &#40;!confirm&#40;'".$GLOBAL&#83;['_LANG']['confirm_cancel']."')) return false;\"&#62;".$GLOBAL&#83;['_LANG']['cancel']."&#60;/a&#62;";
}
else if &#40;$row['order_status'] &#61;&#61; O&#83;_&#83LITED)
{
/* 对配送状态的处理 */
if &#40;$row['shipping_status'] &#61;&#61; &#83;&#83;_&#83;HIPPED)
{
@$row['handler'] &#61; "&#60;a href&#61;\"user.php?act&#61;affirm_received&order_id&#61;" .$row['order_id']. "\" onclick&#61;\"if &#40;!confirm&#40;'".$GLOBAL&#83;['_LANG']['confirm_received']."')) return false;\"&#62;".$GLOBAL&#83;['_LANG']['received']."&#60;/a&#62;";
}
elseif &#40;$row['shipping_status'] &#61;&#61; &#83;&#83;_RECEIVED)
{
@$row['handler'] &#61; '&#60;span style&#61;"color:red"&#62;'.$GLOBAL&#83;['_LANG']['ss_received'] .'&#60;/span&#62;';
}
else
{
if &#40;$row['pay_status'] &#61;&#61; P&#83;_UNPAYED)
{
@$row['handler'] &#61; "&#60;a href&#61;\"user.php?act&#61;order_detail&order_id&#61;" .$row['order_id']. '"&#62;' .$GLOBAL&#83;['_LANG']['pay_money']. '&#60;/a&#62;';
}
else
{
@$row['handler'] &#61; "&#60;a href&#61;\"user.php?act&#61;order_detail&order_id&#61;" .$row['order_id']. '"&#62;' .$GLOBAL&#83;['_LANG']['view_order']. '&#60;/a&#62;';
}
}
}
else
{
$row['handler'] &#61; '&#60;span style&#61;"color:red"&#62;'.$GLOBAL&#83;['_LANG']['os'][$row['order_status']] .'&#60;/span&#62;';
}
$row2 &#61; $db -&#62; getAll&#40;"select goods_id ,goods_sn from ".$ecs-&#62;table&#40;'order_goods')." where order_id &#61; '".$row['order_id']."'");
$row['shipping_status'] &#61; &#40;$row['shipping_status'] &#61;&#61; &#83;&#83;_&#83;HIPPED_ING) ? &#83;&#83;_PREPARING : $row['shipping_status'];
$row['order_status'] &#61; $GLOBAL&#83;['_LANG']['os'][$row['order_status']] . ',' . $GLOBAL&#83;['_LANG']['ps'][$row['pay_status']] . ',' . $GLOBAL&#83;['_LANG']['ss'][$row['shipping_status']];
$arr[] &#61; array&#40;'order_id' &#61;&#62; $row['order_id'],
'order_sn' &#61;&#62; $row['order_sn'],
'order_time'&#61;&#62; local_date&#40;$GLOBAL&#83;['_CFG']['time_format'], $row['add_time']),
'order_status'&#61;&#62; $row['order_status'],
'consignee'&#61;&#62; $row['consignee'],
'total_fee'&#61;&#62; price_format&#40;$row['total_fee'], false),
'goodslist'&#61;&#62; $row2,
'handler'&#61;&#62; $row['handler']);
}
return $arr;
}

16:如何取得ecshop会员一天内的评论条数
回答:
function get_day_count_comment&#40;$user_id &#61; ''){

global $db;
global $ecs;
$pre &#61; local_mktime&#40;0,0,0,date&#40;"m"),date&#40;"d"),date&#40;"y"));
$next &#61; local_mktime&#40;0,0,0,date&#40;"m"),date&#40;"d")+2,date&#40;"y"));

$count &#61; $db -&#62; getOne&#40;"select count&#40;*) from ".$ecs-&#62;table&#40;'comment')." where user_id &#61; '$user_id' and add_time &#62; '$pre' and add_time &#60; '$next' ");

if&#40;$count &#62; 5){
return false;
}else{
return true;
}
}

17:ecshop中js是如何验证radio选择.
回答:类似以下代码
/*
var businesstype &#61; false;
var job_title &#61; false;
var number_of_employees &#61; false;

for &#40;i &#61; 0; i &#60; frm.elements.length; i ++ )
{
if &#40;frm.elements[i].name &#61;&#61; 'businesstype' && frm.elements[i].checked)
{
businesstype &#61; true;
}
if &#40;frm.elements[i].name &#61;&#61; 'job_title' && frm.elements[i].checked)
{
job_title &#61; true;
}
if &#40;frm.elements[i].name &#61;&#61; 'number_of_employees' && frm.elements[i].checked)
{
number_of_employees &#61; true;
}
if &#40;frm.elements[i].name &#61;&#61; 'number_of_employees' && frm.elements[i].checked)
{
number_of_employees &#61; true;
}
}
*/

18:如何增加mysql的连接数量。让ecshop更稳定.
回答:进入mysql命令模式下。执行该&#83;QL语句
set global max_connections&#61;500;
show variables;

19:如何让ecshop模板中的table文字处置居中

回答: style&#61;"vertical-align:middle; " 属性就可以了

20:如何建立msyql一默认字段为时间格式.默认为系统时间.
回答:alter table ecs_b add column age times**p DEFAULT now&#40;)

21:如何控制js只输入数字.
回答:&#60;input name&#61;"quatity" type&#61;"text" class&#61;"input2" value&#61;"0" size&#61;"6" onKeyUp&#61;"value&#61;value.replace&#40;/[^\d]/g,'')"&#62;

22:ecshop中如何用正则去匹配sql语句.
回答:return preg_replace&#40;"/\s*select\s+&#40;\*|[\w_]+&#40;\s*,\s*[\w_]+)*)\s+from\s+[\w_]+&#40;\s+where\s+[\w_]+\s*&#61;\s*\&#83;+)?&#40;\s+order\s+by\s+[\w_]+ &#40;\s+&#40;A&#83;C|DE&#83;C))?)?\s*$/i","",$sql);
23:如何删除ecshop中url特殊字符
回答: if &#40;$rewrite &#61;&#61; 2 && !empty&#40;$append))
{
//$uri .&#61; '-' . urlencode&#40;preg_replace&#40;'/[\.|\/|\?|&|\+|\\\|\'|"|,]+/', '', $append));
//$uri&#61;str_replace&#40;'+','-',$uri);
$str &#61; preg_replace&#40;'/[\.|\/|\?|&|\+|\\\|\'|"|,]+/', '', $append);
$ar&#61; array&#40;'&#40;'&#61;&#62;'',')'&#61;&#62;'','#'&#61;&#62;'','+'&#61;&#62;'-');
$str &#61; strtr&#40;$str,$ar);
$str &#61; preg_replace&#40;"/\s+/","-",$str);
$uri.&#61;$str;
}