title: Ideas to create price range function for a PHP ecommerce website
body: <p>Any ideas to create a function which will return 5 ranges in an array with starting price and end price. The function needs to create these ranges for a category or brand or any filter used. Many e-commerce websites are already using the price range filter. Right now I am able to create function like this. But here I have to add the steps range. Is there a way that these steps can be automatically based on the overall price range for that category.</p>
  
  <pre><code>function price_range($category_id=0, $step=199)
  {   
  $output = '';
  $return = array();
  
  $query = "SELECT * FROM products WHERE category = ".$category_id;
  $query .= " ORDER BY PRICE";
  $result = mysql_query($query);
  
  if (!$result)
  {
      return FALSE;
  }
  else
  {
      if((mysql_num_rows($result) == 0))
      {
          return FALSE;
      }
      else
      { 
          while ($row = mysql_fetch_array($result)) 
          { 
  
              $prices = array($row['price']); 
          } 
          $ranges = array(); 
  
          foreach($prices as $p) 
          { 
              $index = floor($p / $step);  
              if(isset($ranges[$index])) 
                  $ranges[$index]++; 
              else 
                  $ranges[$index] = 1; 
          } 
  
          for($i = 0; $i &lt;= max(array_keys($ranges)); $i++) 
          { 
              //$output = "(&lt;a href=\"?start=" . ($step * $i) . "&amp;end=" . ($step * ($i + 1)) . "\"&gt;" . ($step * ($i + 1)) . " - " . ($step * $i) . "(" . $ranges[$i] . ")&lt;br&gt;";
              $return[$i]['start'] =  ($step * $i);
              $return[$i]['end'] =  ($step * ($i + 1));
              $return[$i]['product_count'] = $ranges[$i];
          }  
      }
  }
  return $return;
  }
  </code></pre>