Location: PHPKode > scripts > Gas mileage calculator > helper.php
<?php
defined('_JEXEC') or die('Direct Access to this location is not allowed.');
 
class ModGasCalcHelper
{
    /**
     * Does the actual calculations
    */
    public function calculate()
    {
        // session storage
    	foreach($_POST as $key=>$var) $_SESSION["calc_gas_".$key]=$var;    	
        $price=round(($_POST['distanceValue']/100) * $_POST['mileageValue'] * $_POST['priceValue'],2);
        
        switch($_POST['distance'])
    	{
    		case 'm': 
    		{
    			$mainUnit="Miles";
    			$mainMileage="Gallons per 100 Miles";
    			$mainPrice="per gallon";
    			$subUnit="Kilometers";
    			$subMileage="litres per 100 Kilometers";
    			$subPrice="per litre";
    			$equivDistance=round($_POST['distanceValue'] * 1.609344);
    			$perunit=round($price/$_POST['distanceValue'],2);
    			$volume=round(($_POST['distanceValue'] / 100) * $_POST['mileageValue'],2);
    			$volumeUnit="gallons";
    			break;
    		}
    		case 'km':
    		{
    			$subUnit="Miles";
    			$subMileage="Gallons per 100 Miles";
    			$subPrice="per gallon";
    			$mainUnit="Kilometers";
    			$mainMileage="litres per 100 Kilometers";
    			$mainPrice="per litre";
    			$equivDistance=round($_POST['distanceValue'] * 0.621371192);
    			$perunit=round($price/$_POST['distanceValue'],2);
    			$volume=round(($_POST['distanceValue'] / 100) * $_POST['mileageValue'],2);
    			$volumeUnit="litres";
    			break;
    		}
    	}	
	
        $message="Your ".$_POST['distanceValue']." $mainUnit ($equivDistance $subUnit) road trip will cost you about $_POST[currency]$price in fuel, based on an efficiency of ".$_POST['mileageValue']." $mainMileage and a total fuel volume of $volume $volumeUnit. (Your fuel cost per $mainUnit will be about $_POST[currency]$perunit)";
        
        return $message;
    } // end calculate
 
} //end ModGasCalcHelper
?>
Return current item: Gas mileage calculator