ecshop 获取商品列表并根据商品的销量进行排序

$act = !empty($_GET['act']) ? $_GET['act'] : '';
$page = isset($_GET['page']) ? $_GET['page'] : 1;
$order = isset($_GET['order']) ? $_GET['order'] : 'asc';
$pagesize = 10;
if (isset($_REQUEST['cid'])) {
        $cat_id = intval($_REQUEST['cid']);
} else {
        die($json->encode(array('code'=>'0', 'msg'=>'获取数据失败')));
}
$total = $db->getOne("SELECT count(*) FROM "  . $ecs->table("goods") . " WHERE cat_id='$cat_id' AND is_on_sale = 1 AND is_alone_sale = 1 AND is_delete = 0");//总条数
$totalpage = ceil($total/$pagesize);//总页数
empty($page) ? $pagestart = 0 : $pagestart = ($page -1) * $pagesize;

if($act=='xl'){
	$goodslist = $db->getAll("SELECT goods_id,goods_name,goods_thumb,shop_price,market_price,brand_id,is_shipping,item_location FROM "  . $ecs->table("goods") . " WHERE cat_id='$cat_id' AND is_on_sale = 1 AND is_alone_sale = 1 AND is_delete = 0 order by shop_price $order");
	foreach ($goodslist as $k => $v) {
	    // $goodslist[$k]['snum'] = $db->getOne('SELECT count(*) FROM' . $ecs->table('order_goods') . ' WHERE goods_id= ' . $goodslist[$k]['goods_id'] . ' AND is_real=1');
	    $goodslist[$k]['snum'] = ec_buysum($goodslist[$k]['goods_id']);
    }
    $sumnumber = array_column($goodslist,'snum');
    if($order == 'asc'){
    	array_multisort($sumnumber,SORT_ASC,$goodslist);//array_multisort 可以用来一次对多个数组进行排序,或者根据某一维或多维对多维数组进行排序。
    }
    if($order=='desc'){
    	array_multisort($sumnumber,SORT_DESC,$goodslist);//array_multisort 可以用来一次对多个数组进行排序,或者根据某一维或多维对多维数组进行排序。
    }
   $goodslist = array_slice($goodslist,$pagestart,$pagesize);//array_slice() 函数在数组中根据条件取出一段值,并返回。
}

foreach ($goodslist as $k => $v) {
	$goodslist[$k]['zhijiang'] = $goodslist[$k]['market_price']-$goodslist[$k]['shop_price'];
    $goodslist[$k]['goods_thumb'] = 'https://' . $_SERVER['SERVER_NAME']. '/' .$goodslist[$k]['goods_thumb'];
	$goodslist[$k]['snum'] = ec_buysum($goodslist[$k]['goods_id']);
}
if($goodslist){
	die(json_encode(array('code' => 1,'msg'=>'成功','data'=>$goodslist)));
}else{
	die(json_encode(array('code' => 0,'msg'=>'失败','data'=>'')));
}

function ec_buysum($goods_id)
{
    $LMonth=strtotime("last month"); //前一个月
    $nowTime=time(); //当前时间
    $sql="select sum(goods_number) from " . $GLOBALS['ecs']->table('order_goods') . " AS g ,".$GLOBALS['ecs']->table('order_info') . " AS o WHERE o.order_id=g.order_id and g.goods_id = ".$goods_id." and o.pay_status=2 and o.add_time >= ".$LMonth." and o.add_time <= ".$nowTime." group by g.goods_id";
    if (($GLOBALS['db']->getOne($sql)) == ""){
            return "0";
        }else{
       return $GLOBALS['db']->getOne($sql);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值