//取出分类下的筛选属性 $cateId=I('get.cid'); /********计算这个分类下商品的七个价格区间的范围******/ $goodsModel = D("Goods"); $goodsPriceData=$goodsModel->field('MIN(shop_price) minprice,MAX(shop_price) maxprice,GROUP_CONCAT(shop_price) as priceStr') ->where(array( 'cat_id'=>array('eq',$cateId), 'is_delete'=>array('eq',0), 'is_on_sale'=>array('eq',1), ))->find(); //算法:计算商品价格的七个区间 $priceNumber=7; $sprice=ceil(($goodsPriceData['maxprice']-$goodsPriceData['minprice'])/$priceNumber); $firsetPrice = $goodsPriceData['minprice']; //接收七个区间的价格范围 $_priceNumber=array(); for($i=0;$i<$priceNumber;$i++){ if($i<($priceNumber-1)) $_priceNumber[]=(floor($firsetPrice/10)*10).'-'.(floor(($firsetPrice+$sprice)/10)*10-1); else $_priceNumber[]=(floor($firsetPrice/10)*10).'-'. ceil($goodsPriceData['maxprice']/10)*10; $firsetPrice+=$sprice; } //把从商品中取出来的价格字符串转化成数组后, $goodsPrice=explode(',',$goodsPriceData['priceStr']); sort($goodsPrice); //在价格区间中做比对,如果区间中有商品保存价格区间,否则删除 foreach($_priceNumber as $k => $v){ $a = explode('-', $v); $start=$a[0]; $end =$a[1]; $panduan=array(); foreach($goodsPrice as $k1 => $v1){ $v1=floor($v1); //价格在此区间,把该价格保存在数组中 if($v1>=$start && $v1<=$end) $panduan[]=$v1; } //如果取出的商品没有在此价格区间的,删除该区间范围 if(empty($panduan)) unset($_priceNumber[$k]); }