# How to make price range?

Hello,

I have table with articles and price

## articles

id | title | price

examples data:

## articles

1 | book #1 | 95.00
2 | book #2 | 125.00
3 | book #3 | 235.00

i want to print something like this:

Select price range:

\$0 - \$100
\$100 - \$235

AS you can see i need to watch for max price and i would like option to show custom number of range for example

2 range:

\$0 - \$100
\$100 - \$235

3 range:

\$0 - \$50
\$50 - \$100
\$100 - \$235

You can get the highest price with SELECT MAX(price)

With your example the rules are to arbitrary to do this automatically. e.g. highest price is \$9000, so 3 range would become:
0-50
50-100
100-9000

You could have a bunch of ranges and test how many options each will give though.

So youd need to use MIN() too, because that first range would bring back 0 results (I really hate websites that permit that).

By using MIN and MAX as start and endpoints you could conceivably do something like this:

\$ranges = range( \$min, \$max, 50 );

Which’d give you starting array of values.

``````
// \$min, \$max are the results of MIN() MAX() query in your dbase
// cleaned up (chop pennies off etc), and turned into integers

\$min = 95 ;
\$max = 235 ;

\$ranges = range( \$min, \$max, 50) ;

var_dump ( \$ranges ) ;

// array
//  0 => int 95
//  1 => int 145
//  2 => int 195

``````

One could imagine rounding up the \$min to 100 and starting the range from there, then looping through the \$ranges so you end up with

95
150
200

Any luck with it? I’m having a similar issue and the range function does not do the trick

What are you trying to do? What code do you have so far?

You can give this a whirl.

``````
<?php
function getRanges(\$intMin,\$intMax,\$intRanges=3) {

\$intRange = \$intMax-\$intMin;
\$intIncrement = abs(\$intRange/\$intRanges);
\$arrRanges = array();

for(\$i=0;\$i<\$intRanges;\$i++) {
\$arrRanges[] = \$i==0 || \$i==(\$intRanges-1)?\$i==0?\$intMin:\$intMax:\$intMin+(\$i*\$intIncrement);
}

return \$arrRanges;

}

function renderRanges(\$arrRanges,\$strSelected='',\$strName='ranges') {

printf('<select name="&#37;s">',\$strName);
foreach(\$arrRanges as \$intIndex=>\$intRange) {

\$intMin = \$intIndex == 0?\$intRange:\$arrRanges[(\$intIndex-1)];
\$intMax = \$intIndex == 0?\$arrRanges[(\$intIndex+1)]:\$intRange;
\$boolSelected = strcmp("\$intMin-\$intMax",\$strSelected) == 0?true:false;

printf(
'<option value="%u-%u"%s>%1\$u&ndash;%2\$u</option>'
,\$intMin
,\$intMax
,\$boolSelected === true?' selected="selected"':''
);
}
echo '</select>';
}

renderRanges(getRanges(95,235,3));
?>

``````

I’m trying to have an array of prices as input and the output result for a set like 56,76,103,149,159,239,289, would be something like array([0]=>array([min]=>39,[max]=>59),array([min]=>59,[max]=>79),array([min]=>99,[max]=>199),array([min]=>199,[max]=>299))

what i had in mind was for the function to accept the prices values, iterate them an place them in the respective range. This is one of the many different approaches i tried out. It is not prety nor does it work correctly but this is the big idea
function price_range(\$items,\$index=1)
{
\$maxPrice = ceil(max(\$items));
for (\$i=0,\$j=strlen(floor(\$maxPrice));count(\$items) >\$i;\$i++)
{
\$range = pow(10, (strlen(floor(\$items[\$i]))-\$index));
\$tmp[\$range] = \$items[\$i];
}
if (count(\$tmp) == 1) {
\$tmp = array();

return \$tmp;
}
else {
return \$tmp;
}

}

The input is a flat array of all the prices. The output is an array of the ranges. Which works great if all your prices are in different powers of 10, but fails if they are on the same power.