Price Range for products list


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. $products_count = $result->RecordCount();
  2. $productArray = $result->GetArray();
  3. $productSum = 0;
  4. foreach($productArray as $price){
  5. $productSum = $productSum + intval($price['products_price']);
  6. }
  7.  
  8. $median = intval($productSum/$products_count);
  9. if ($median > 100){
  10. $median = intval($median/100);
  11. $median = $median*100;
  12. }
  13.  
  14. $second_price = $median;
  15.  
  16. $lower_count = 0;
  17. $lower_median_sum =0;
  18. foreach ($productArray as $price){
  19. if ($price['products_price'] < $median){
  20. $lower_count++;
  21. $lower_median_sum = $price['products_price'] + $lower_median_sum;
  22. }
  23. }
  24. if ($lower_count > 1){
  25. $first_price = intval($lower_median_sum/$lower_count);
  26. }
  27.  
  28.  
  29. $upper_count = 0;
  30. $upper_median_sum = 0;
  31. foreach ($productArray as $price){
  32. if ($price['products_price'] > $median){
  33. $upper_count++;
  34. $upper_median_sum = $price['products_price'] + $upper_median_sum;
  35. }
  36. }
  37.  
  38. if ($upper_count > 1){
  39. $third_price = intval($upper_median_sum/$upper_count);
  40. }
  41.  
  42.  
  43. if ($first_price > 100){
  44. $first_price = intval($first_price/100);
  45. $first_price = $first_price*100;
  46. }
  47.  
  48. if ($third_price > 100){
  49. $third_price = intval($third_price/100);
  50. $third_price = $third_price*100;
  51. }
  52.  
  53.  
  54. //return self::getPriceRangeArray($row['totalProduct'],$row['sum'],$productStatus,$brandID,$categoryIds);
  55.  
  56. $priceRangeArray = array();
  57. $i=0;
  58. if($products_count > 1 ){
  59. if (isset($first_price)){
  60. if(self::checkPriceRange(0,$first_price,$productStatus,$brandID,$categoryIds)>0) {
  61. $priceRangeArray[$i]['range'] = '0-'.$first_price;
  62. $priceRangeArray[$i]['label'] = 'Below Rs.'.$first_price;
  63. $i++;
  64. }
  65. if(self::checkPriceRange($first_price,$second_price,$productStatus,$brandID,$categoryIds)>0) {
  66. $priceRangeArray[$i]['range'] = $first_price.'-'.$second_price;
  67. $priceRangeArray[$i]['label'] = 'Rs.'.$first_price .' - Rs.'.$second_price;
  68. $i++;
  69. }
  70. } else {
  71. if(self::checkPriceRange(0,$second_price,$productStatus,$brandID,$categoryIds)>0) {
  72. $priceRangeArray[$i]['range'] = '0-'.$second_price;
  73. $priceRangeArray[$i]['label'] = 'Below Rs.'.$second_price;
  74. $i++;
  75. }
  76. }
  77. if (isset($third_price)){
  78. if(self::checkPriceRange($second_price,$third_price,$productStatus,$brandID,$categoryIds)>0) {
  79. $priceRangeArray[$i]['range'] = $second_price.'-'.$third_price;
  80. $priceRangeArray[$i]['label'] = 'Rs.'.$second_price .' - Rs.'.$third_price;
  81. $i++;
  82. }
  83. if(self::checkPriceRange($third_price,999999,$productStatus,$brandID,$categoryIds)>0) {
  84. $priceRangeArray[$i]['range'] = $third_price.'-99999999';
  85. $priceRangeArray[$i]['label'] = 'Rs.'.$third_price.' Above';
  86. $i++;
  87. }
  88. } else {
  89. if(self::checkPriceRange($second_price,999999,$productStatus,$brandID,$categoryIds)>0) {
  90. $priceRangeArray[$i]['range'] = $second_price.'-99999999';
  91. $priceRangeArray[$i]['label'] = 'Rs.'.$second_price.' Above';
  92. $i++;
  93. }
  94. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.