How to create Price Range dynamically with PHP

前端 未结 4 1702
孤街浪徒
孤街浪徒 2021-01-13 05:36

How can I create Price Ranges from price array? Let\'s say I have this array which holds prices:

  Array ( [0] => 500 [1] => 500 [2] => 520 [3] =&g         


        
4条回答
  •  孤城傲影
    2021-01-13 05:42

    I made it automated; i didnt use classes but it is easily able to be converted.

    $maxPRoductInRange){
                //create bigger range chart
                makeRangeChart($min,$max,$rangeChart);
                calculateRange();
            } 
    
        }
    }
    }
    
    
    function checkProductCount($min,$max,$priceList){
        global $chart;
        $count=0;
        $rest=0;
    
        foreach( $priceList as $price){
            if($price>=$min && $price<$max) { 
                $count++; 
            } else { $rest++; }
    
        }
        $chart[$min]=$count;
    
        return array($count,$rest);
    }
    
    
    function makeRangeChart($min=0,$max,&$rangeChart){
        $middleOfRange=($max+$min)/2;
        $rangeChart[]=$min;
        $rangeChart[]=$middleOfRange;
        $rangeChart[]=$max;
        $rangeChart=array_unique ($rangeChart);
        sort($rangeChart, SORT_NUMERIC );
    }
    
    function printChart(){
    global $chart,$highestPrice;
    
    $minPrices=array_keys($chart);
    $count=count($minPrices);
    $line='';
        for($a=0;$a<$count;$a++){
            $line.=$minPrices[$a];
            $line.=(isset($minPrices[$a+1]))?' - '.$minPrices[$a+1]:'+';
            $line.='('.$chart[$minPrices[$a]].')
    '; } return $line; } calculateRange(); echo printChart(); ?>

提交回复
热议问题