我正在尝试在我的系统中选择价格清单。价格存储为整数。我能够获得最低的价格和最高的价格,但我想在选择列表中显示它们。我不希望选择列表缓慢增加但是增加100或10,000或100,000,这取决于我的起始编号是什么,我在增量处。
例如,假设我有这两个价格:
500000
12345689
我正在尝试将它们增加100,000,然后当我达到1,000,000时,我想增加它。它看起来像这样:
500000
600000
700000
800000
900000
1000000
2000000
我正在使用自定义功能和一些格式来获取所有价格并获得我的起始价格和最终价格:
$prices = my_custom_function(); // Pulls All Prices in a random order
if(!empty($prices)){
sort($prices); // Sort Prices Lowest to Highest
$price_low = $prices[0];
$price_high = $prices[count($prices)-1];
$price_start = intval( $price_low[0].str_repeat( '0', strlen( $price_low ) - 1 ) );
$price_end = intval( ( $price_high[0] + 1 ).str_repeat( '0', strlen( $price_high ) -1 ) );
}
使用上面的相同示例,我的起始价格和最终价格将是:
$price_start = 500000
$price_end = 20000000
现在它处于循环中,我遇到麻烦,用我想要的值递增它。我正在尝试使用while循环并确定我在增量器中的位置:
<?php $i = $price_start; $x = 0; while($x < 10) : ?>
<option value="<?php echo $i; ?>"><?php echo format_price($i); ?></option>
<?php
if(1000 % $i == 0)
$i+=1000;
else if(10000 % $i == 0)
$i+=10000;
else if(100000 % $i == 0)
$i+=100000;
else if(1000000 % $i == 0)
$i+=1000000;
else
$i+=10000000;
$x++;
endwhile;
?>
我最后添加了x变量,因为我一直遇到无限循环问题,但理论上它应该是while($i <= $price_end)
。有人能指出我如何获得预期产量的正确方向吗?我觉得我很接近,但还没到那里,而且可能有更好/更快的方式去做。任何帮助都会很棒。
我想一个简单的方法是:
1 -> +1
10 -> +10
100 -> +100
1000 -> +1000
10000 -> +10000
等等。
答案 0 :(得分:2)
log10(1234); // 3.09131
floor(log10(1234)); // 3
pow(10,floor(log10(1234))); // 1000
答案 1 :(得分:1)
如果有人需要完整的解决方案,那就是:
$price = 100; // Starting Price
$priceEnd = 10000; // Ending Price
while($price <= $priceEnd) {
echo $price . "<br/>";
$increase = pow(10,floor(log10($price)));
$price = $price + $increase;
}