/**
* 随机获取商品
*/
public function getRandGoods(){
$map['status'] = 1; //状态;上架
$map['num'] = ['>','0'];//库存
$query = model('goods');
$about = $query
->where($map)
->count();
$limit = 20; //抽取数量
$toffset=range(0,$about);
shuffle($toffset);
if($toffset){
$st = $toffset[0]; //随机第几个开始
$lens = $about - $st ; //个数是否足够
if($lens < $limit){
$st = $limit - $lens;
}
if($st <=0 || $st <= $limit){
$st = 0;
}
}
unset($map);
$map['g.status'] = 1; //状态;上架
$map['g.num'] = ['>','0'];//库存
$l_list = $query //获取需要随机条件的数组
->alias('g')
->where($map)
->order('g.recommend desc,g.addtime desc,g.virtual_sales desc')
->join('shop s','s.id = g.shop_id')
->field('g.*,s.district_name')
->limit($st,$limit)
->select();
$len = count($l_list); //统计符合条件的数组个数
$len = $len-1;
$arr=range(0,$len);
shuffle($arr);//模拟数组,随机下标
$look_list =array();
foreach($arr as $values)
{
$look_list[] = $l_list[$values];
if(count($look_list) == 10){ //获得数量
break;
}
}
\Org\Response::show(200,'access!', $look_list);
}
版权声明:本文为qq_32562501原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。