Saturday, January 24, 2009

Php - Calculating Price Ranges Dynamicaly if minimum and maximum values are given.

Calculating Price Ranges Dynamicaly if minimum and maximum values are given.
In these example display 5 types of ranges


<?php
//Find Minimum and Maximum Values
$min = 20.55;//Minimum Price
$max = 10000.89;//Maximum Price
$numofRanges = 5;
//Split the numbers in to 5 price ranges
/*For eg
Price 20$ -----------200$
200$-----------400$
400$-----------600$
*/
$value = (float)($max/$numofRanges);
//Find the value to increment
$range = array();
$min_temp = $min;
for(
$i=1;$i<=$numofRanges;$i++)
{
$temp = $min + $value;
if(
$temp > $max)
{
$temp = $max;
}
if(
$min_temp != $min)
{
$min_temp = round($min_temp);
}
if(
$temp != $max)
{
$temp = round($temp);
}
$range["$min_temp"] = $temp;
$min_temp = $temp;
$min_temp += 1;
$min = $temp;
}
$str = '';
foreach(
$range as $min=>$max)
{
$str .= $min."\$--------------".$max."\$";
$str .= "<br/>";
}
echo
"<h3>Price Ranges</h3>";
echo
$str;

?>

No comments: