Web Analytics Made Easy -
StatCounter Creating a range of numbers - CodingForum

Announcement

Collapse
No announcement yet.

Creating a range of numbers

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Creating a range of numbers

    Hi Folks,

    I'm trying to create a price range filter where all I have to start with is the MIN and MAX values of the prices in the db.

    The min and max change depending on what range of products your viewing.

    I want to create a drop down filter so that ppl can filter the results.

    like Price Range : 0 - 100, 100 - 200, 200 - 300 etc.

    Obviously the range needs to be relevant to the MIN and MAX, for example if the MIN was 10 and the MAX 200 i would want

    0 - 10
    10 - 20
    20 - 30
    30 - 40
    40 - 50
    50 - 100
    100 - 200

    Type of thing. Hope this makes sense, i've attached my code that doesnt quite work.

    Code:
    if($iMin < 100)
    {
    	$iIncrement = 10;
    }
    else if($iMin < 1000)
    {
    	$iIncrement = 100;
    }
    else
    {
    	$iIncrement = 500;
    }
    
    for($i = 0; $i <= $iMax; $i += $iIncrement)
    {
    	echo $i." - ".($iIncrement+$i)."(".$iIncrement.")<br />";
    
    	if($i < 100)
    	{
    		$iIncrement = 10;
    	}
    	else if($i < 800)
    	{
    		$iIncrement = 100;
    	}
    	else if($i > 1000)
    	{
    		$iIncrement += 500;
    	}
    }

  • #2
    It depends, do you want to make sure the range values stay with multiples of 10 or 100? Or does that matter?

    To just get an balanced split of ranges, you'll need to specify how many ranges you want and then do some calculatin'.
    PHP Code:
    $rangeCount 50;
    $min 500;
    $max 1000;
    $interval round(($max $min) / $rangeCount);
    for (
    $i $min$i $max$i $i $interval) {
        
    $interval = (int) round(($max $i) / $rangeCount);
        
    $topOfRange $i $interval 1;
        echo 
    "$i - $topOfRange<br />";
        
    $rangeCount--;

    Comment


    • #3
      range()
      Stupid PHP has a function for everything...
      I'm not sure if this was any help, but I hope it didn't make you stupider.

      Experience is something you get just after you really need it.
      PHP Installation Guide Feedback welcome.

      Comment


      • #4
        Now that is funny! It's why I like this forum, I keep learning new stuff.

        Comment


        • #5
          Yeah I'm aware of range() but when using php 4.1 it doesnt support the $step parameter.

          Just to note, I would want the increment to alter depending on the values being passed in, so if were in the 1000s then the increment would be 1000, over 5000 and the increment 5000 etc.

          Matt

          Comment


          • #6
            The following requires a little hard coding...but how big are your numbers really going to get? Hope it's helpful.

            PHP Code:
                function getIntRanges($min=0,$max=100)
                {
                    if(
            $max <= $min)
                        die(
            'Invalid min/max');

                    
            $ranges    = array();

                    while(
            $min $max)
                    {
                        
            # Auto detect next step
                        
            switch(strlen($min))
                        {
                            case 
            1# 0-9
                            
            case 2# 10-99
                                
            $step $min 50 10 50;
                            break;
                            case 
            3# 100-999
                                
            $step $min 500 100 250;
                            break;
                            case 
            4# 1000-9999
                                
            $step $min 5000 1000 2500;
                            break;
                            case 
            5# 10000-99999
                                
            $step $min 50000 10000 25000;
                            break;
                            default:
                                
            $step 100000;
                            break;
                        }

                        if(
            $min+$step $max)
                            
            $ranges[] = $min.'-'.$max;
                        else
                            
            $ranges[] = $min.'-'.($min+$step);

                        
            $min += $step;
                    }

                    return 
            $ranges;

                } 
            # End function getIntRanges()

                
            print_r(getIntranges(0,999999)); 
            Output:
            Code:
            Array
            (
                [0] => 0-10
                [1] => 10-20
                [2] => 20-30
                [3] => 30-40
                [4] => 40-50
                [5] => 50-100
                [6] => 100-200
                [7] => 200-300
                [8] => 300-400
                [9] => 400-500
                [10] => 500-750
                [11] => 750-1000
                [12] => 1000-2000
                [13] => 2000-3000
                [14] => 3000-4000
                [15] => 4000-5000
                [16] => 5000-7500
                [17] => 7500-10000
                [18] => 10000-20000
                [19] => 20000-30000
                [20] => 30000-40000
                [21] => 40000-50000
                [22] => 50000-75000
                [23] => 75000-100000
                [24] => 100000-200000
                [25] => 200000-300000
                [26] => 300000-400000
                [27] => 400000-500000
                [28] => 500000-600000
                [29] => 600000-700000
                [30] => 700000-800000
                [31] => 800000-900000
                [32] => 900000-999999
            )
            Last edited by syosoft; Oct 1, 2006, 05:43 AM. Reason: Forgot php bb tags
            Active PHP/MySQL application developer available for immediate work.
            syosoft.com mavieo.com - Remote Web Site Administration Suite - Reseller Ready

            Comment

            Working...
            X