$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);
}
}
03-07
525