Calculator - PHP Online

Form of PHP Sandbox

Enter Your PHP code here for testing/debugging in the Online PHP Sandbox. As in the usual PHP files, you can also add HTML, but do not forget to add the tag <?php in the places where the PHP script should be executed.



Your result can be seen below.

Result of php executing





Full code of Calculator.php

  1. <?php
  2.  
  3. function getOptions($type){
  4.     $options = [
  5.         'Split-System' => [
  6.             'availableTypes' => ['Split System A/C Condensers'],
  7.             'mainBtu' => 'cbtu',
  8.         ],
  9.         'Ductless Mini-Split' => [
  10.             'availableTypes' => ['Mini-Split'],
  11.             'mainBtu' => 'cbtu',
  12.         ],
  13.         'Self-Contained Package' => [
  14.             'availableTypes' => ['Gas Furnace', 'Package Gas', 'Package Heat Pump', 'Packaged A/C'],
  15.             'mainBtu' => 'cbtu',
  16.         ],
  17.         'Geothermal' => [
  18.             'availableTypes' => ['Split System Heat Pump', 'Modular Blower', 'Evaporator Coils'],
  19.             'mainBtu' => 'cbtu',
  20.         ],
  21.     ];
  22.  
  23.     return $options[$type];
  24. }
  25.  
  26. function getBtuForSqft(){
  27.     return [
  28.         'cbtu' => [
  29.             'basic' => [
  30.                 '1' => 25,
  31.                 '2' => 24,
  32.                 '3' => 24,
  33.                 '4' => 22.5,
  34.                 '5' => 20,
  35.                 '6' => 19,
  36.                 '7' => 18,
  37.             ],
  38.             'above' => [
  39.                 '1' => 18,
  40.                 '2' => 17,
  41.                 '3' => 16,
  42.                 '4' => 15,
  43.                 '5' => 13.25,
  44.                 '6' => 12.5,
  45.                 '7' => 12,
  46.             ],
  47.             'outstanding' => [
  48.                 '1' => 14.15,
  49.                 '2' => 14.15,
  50.                 '3' => 14.15,
  51.                 '4' => 13.275,
  52.                 '5' => 11.8,
  53.                 '6' => 11.21,
  54.                 '7' => 10.62,
  55.             ],
  56.         ],
  57.         'hbtu' => [
  58.             'basic' => [
  59.                 '1' => 0,
  60.                 '2' => 30,
  61.                 '3' => 35,
  62.                 '4' => 40,
  63.                 '5' => 45,
  64.                 '6' => 50,
  65.                 '7' => 55,
  66.             ],
  67.             'above' => [
  68.                 '1' => 0,
  69.                 '2' => 22.6,
  70.                 '3' => 25.2,
  71.                 '4' => 28.8,
  72.                 '5' => 32.4,
  73.                 '6' => 36,
  74.                 '7' => 39.6,
  75.             ],
  76.             'outstanding' => [
  77.                 '1' => 0,
  78.                 '2' => 17,
  79.                 '3' => 20,
  80.                 '4' => 23,
  81.                 '5' => 25.5,
  82.                 '6' => 28.3,
  83.                 '7' => 31.15,
  84.             ],
  85.         ]
  86.     ];
  87. }
  88.  
  89. function getClimateZoneApi($zipCode)
  90. {
  91.     $json = file_get_contents('http://www.afanalytics.com/api/climatezone/' . $zipCode);
  92.     if($json) {
  93.         $data = json_decode($json);
  94.         return $data->climate_zone_number;
  95.     }
  96.  
  97.     return null;
  98. }
  99.  
  100.  
  101. //Sort an array of xml objects
  102. function xsort(&$nodes, $child_name, $order = SORT_ASC)
  103. {
  104.     $sort_proxy = array();
  105.  
  106.     foreach ($nodes as $k => $node)
  107.     {
  108.         $sort_proxy[$k] = (string) $node->$child_name;
  109.     }
  110.  
  111.     array_multisort($sort_proxy, $order, $nodes);
  112. }
  113.  
  114. function getProducts($type, $btuArray){
  115.     $options = getOptions($type);
  116.     $types = $options['availableTypes'];
  117.     $btu_type = $options['mainBtu'];
  118.     $btu = $btuArray[$btu_type];
  119.     $result = [];
  120.     $temp = [];
  121.  
  122.     $productsXml = simplexml_load_file('https://mrcool.com/feeds/sysselect/sysselect.xml');
  123.  
  124.     if ($productsXml == null) {
  125.         echo(json_encode($result));
  126.         wp_die();
  127.     }
  128.  
  129.     foreach ($productsXml->product as $product) {
  130.         if (in_array($product->type, $types) && $btu) {
  131.             $productBtu = (int)$product->$btu_type;
  132.             getBtuRange($temp, $productBtu, $btuArray[$btu_type]);
  133.         }
  134.     }
  135.  
  136.     correctBtuRange($temp, $btuArray[$btu_type]);
  137.     $types[] = 'Air Handler';
  138.  
  139.     foreach ($productsXml->product as $product) {
  140.         $productBtuArray = explode("-", $product->$btu_type);
  141.  
  142.         if (in_array($product->type, $types) && $btu) {
  143.  
  144.             //Finding exact matches
  145.             if (count($productBtuArray) == 1) {
  146.                 if($productBtuArray[0] >= $temp['minBtu'] && $productBtuArray[0] <= $temp['maxBtu']) {
  147.                     $result[] = $product;
  148.                 }
  149.             }else{
  150.                 if($productBtuArray[0] >= $temp['minBtu'] && $productBtuArray[1] <= $temp['maxBtu']){
  151.                     $result[] = $product;
  152.                 }
  153.             }
  154.         }
  155.     }
  156.  
  157.     if($btu_type != 'hbtu'){
  158.         check_hbtu($result, $btuArray['hbtu']);
  159.     }
  160.  
  161.     xsort($result, $btu_type, SORT_ASC);
  162.  
  163.     return $result;
  164. }
  165.  
  166. function check_hbtu(&$result, $hbtu){
  167.     foreach ($result as $product){
  168.         if($product->hbtu < $hbtu){
  169.             $product->error = 'You will might require additional source of heating';
  170.         }
  171.     }
  172. }
  173.  
  174. //Search 4 products with maximum match.
  175. function getBtuRange(&$temp, $productBtu, $btu){
  176.  
  177.     if($btu && $productBtu){
  178.         if(empty($temp['minBtu']) && $productBtu < $btu) {
  179.             $temp['minBtu'] = $productBtu;
  180.         }elseif($productBtu > $temp['minBtu'] && $productBtu < $btu){
  181.             $temp['minBtu'] = $productBtu;
  182.         }
  183.  
  184.         if(empty($temp['maxBtu']) && $productBtu > $btu) {
  185.             $temp['maxBtu'] = $productBtu;
  186.         }elseif($productBtu < $temp['maxBtu'] && $productBtu > $btu){
  187.             $temp['maxBtu'] = $productBtu;
  188.         }
  189.     }
  190. }
  191.  
  192. function correctBtuRange(&$temp, $btu){
  193.     if(!isset($temp['minBtu'])) $temp['minBtu'] = $btu;
  194.     if(!isset($temp['maxBtu'])) $temp['maxBtu'] = $btu;
  195. }
  196.  
  197.  
  198. function getAddress($zipCode)
  199. {
  200.     $json = file_get_contents('https://maps.googleapis.com/maps/api/geocode/json?address=' . $zipCode . '&sensor=true&key=AIzaSyDCtjwqtJRcsplfP1wfZ9RfH2fVOYU3cCc');
  201.     if($json) {
  202.         $data = json_decode($json);
  203.  
  204.         return $data->results[0]->formatted_address;
  205.     }
  206.  
  207.     return null;
  208. }
  209.  
  210. function get_ingram_url_callback()
  211. {
  212.     $result['status'] = 'error';
  213.     $result['url'] = '';
  214.     $result['message'] = '';
  215.  
  216.     if (isset($_POST['data']) && ! empty($_POST['data'])) {
  217.         $params = [];
  218.         parse_str($_POST['data'], $params);
  219.  
  220.         $climateZone = getClimateZoneApi($params['zipCode']);
  221.         $type = $params['type'];
  222.         $insulation = $params['insulationValue'];
  223.         $sqft = $params['square'];
  224.         $btuArray = [];
  225.  
  226.         $BtuArray = getBtuForSqft();
  227.  
  228.         foreach ($BtuArray as $key => $btu){
  229.             $btuArray[$key] = $btu[$insulation][$climateZone] * $sqft;
  230.             $result['message'] .= "$key - $btuArray[$key], ";
  231.         }
  232.  
  233.         $result['status'] = 'ok';
  234.         $result['message'] .= "climate-zone - $climateZone. Address - " . getAddress($params['zipCode']);
  235.         $result['products'] = getProducts($type, $btuArray);
  236.     }
  237.  
  238.     echo(json_encode($result));
  239.     wp_die();
  240. }
  241.  
  242. ?>
File Description
  • Calculator
  • PHP Code
  • 01 Jun-2021
  • 6.58 Kb
You can Share it: