php 商品价格区间表,电商搜索价格区间计算

效果如下图

图一

f62486c4cb9e?from=groupmessage&isappinstalled=0

image.png

图二

f62486c4cb9e?from=groupmessage&isappinstalled=0

image.png

/**

*

*/

class Goods

{

/**

* @brief 计算商品的价格区间

* @param $min 最小价格

* @param $max 最大价格

* @param $showPriceNum 展示分组最大数量

* @return array 价格区间分组

*/

public function getGoodsPrice($min,$max,$showPriceNum = 5){

$goodsPrice = array("min" => $min,"max" => $max);

if($goodsPrice['min'] == null && $goodsPrice['max'] == null)

{

return array();

}

//计算商品价格区间

$perPrice = ceil(($goodsPrice['max']-$goodsPrice['min'])/$showPriceNum);

//返回数据组装

$result = [];

if($perPrice > 0){

$result = ['0-'.$perPrice]; //定义第一个区间

$stepPrice = $perPrice;

for ($addPrice = $stepPrice+1; $addPrice < $goodsPrice['max'];) {

if(count($result) == $showPriceNum){

break;

}

//下个区间结束值

$stepPrice = $addPrice + $perPrice;

//除去首个数字外 剩下所有为 9 效果 2999

$stepPrice = substr(intval($stepPrice), 0, 1).str_repeat('9',(strlen(intval($stepPrice)) - 1));

$result[] = $addPrice.'-'.$stepPrice; //下个区间段

$addPrice = $stepPrice + 1; // 下个区间开始 当前最大值 +1

}

//置换max价格 把数据最后的 值 替换为 最大值

$result[count($result)-1] = str_replace("-".$stepPrice,"-".ceil($goodsPrice['max']),$result[count($result)-1]);

return $result;

}

}

}

$price = new Goods();

$min = 85;

$max = 10056;

$num = 6;

$result = $price->getGoodsPrice($min, $max, $num);

var_dump($result);

array (size=6)

0 => string '0-1662' (length=6)

1 => string '1663-3999' (length=9)

2 => string '4000-5999' (length=9)

3 => string '6000-7999' (length=9)

4 => string '8000-9999' (length=9)

5 => string '10000-10056' (length=11)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值