PHP Calculations

driveflexfuel

New Member
Messages
159
Reaction score
0
Points
0
I have been working on this form to the best of my abilities. I had it programmed in javascript but I do not know php. The code below is my best try but I cant seem to get it to work. I receive a couple errors and all results are 0. Any help is greatly appreciated.

Code:
<?php
$GASPrice = strip_tags($_POST["GASPrice"]);
$Ethanol = strip_tags($_POST["Ethanol"]);
$MPGGAS = strip_tags($_POST["MPGGAS"]);
$Tank = strip_tags($_POST["Tank"]);
$MPGE85= strip_tags($_POST["MPGE85"]);
      if ($MPGE85 <= 0)
        $E85Percent = ($MPGGAS / ($MPGGAS * .87));
      else
        $E85Percent = ($MPGGAS / $MPGE85);
        $GASTank = ($GASPrice * $Tank);
        $E85 = ($Ethanol * 2) / 2;
        $E85TankNon = ($E85 * $Tank);
        $E85Tank = (($E85 * $Tank) * $E85Percent);
        $GasSaveTank = ($E85Tank - $GASTank);
        $GasSaveGall = ($E85Tank - $GASTank) / $Tank;
        $E85SaveTank = ($GASTank - $E85Tank);
        $E85SaveGall = ($GASTank - $E85Tank) / $Tank;
        $Diff = (GASTank - E85Tank);
        $SaveTankGas = "$" + round($GasSaveTank,2);
        $SaveGallGas = "$" + round($GasSaveGall,2);
        $SaveTankE85 = "$" + round($E85SaveTank,2);
        $SaveGallE85 = "$" + round($E85SaveGall,2);
        $E85Tank1 = "$" + round($E85Tank,2);
        $GASTank1 = "$" + round($GASTank,2);
        $E85TankNon1 = "$" + round($E85TankNon,2);
      if (Diff <= 0)
        echo "<table>
                 <tr>
                   <td colspan=2 align=center>Your best choice is using Gasoline.</td>
                 </tr>
                 <tr>
                   <td>Savings per tank</td>
                   <td>" . $SaveTankGas . "</td>
                 </tr>
                 <tr>
                   <td>Savings per gallon</td>
                   <td>" . $SaveGallGas . "</td>
              </table>";
      else
        echo "<table>
                 <tr>
                   <td colspan=2 align=center>Your best choice is using E85.</td>
                 </tr>
                 <tr>
                   <td>Savings per tank</td>
                   <td>" . $SaveTankE85 . "</td>
                 </tr>
                 <tr>
                   <td>Savings per gallon</td>
                   <td>" . $SaveGallE85 . "</td>
                </tr>
             </table>";

    echo  "<table>
             <tr>
                <td>Cost to fill up with Gasoline</td>
                <td>" . $GASTank1 . "</td>
             </tr>
             <tr>
                 <td>Cost to fill up with E85</td>
                <td>" . $E85Tank1 . "</td>
             </tr>
             <tr>
                 <td>Cost to fill up with E85 *Adjusted Price</td>
                <td>" . $E85TankNon1 . "</td>
             </tr>
            </table>";
    
    echo "* Adjusted Price : The adjusted price includes the extra cost due to loss in fuel mileage.";
?>
 
Last edited:

Jake

Developer
Contributors
Messages
4,057
Reaction score
5
Points
0
Try this code, you need to wrap if-else statements with { and a } if there is more than one line of code to be executed after the if and else. You also don't need to make 3 echos you can do it with just one.

PHP:
<?php
// This is the input from the form -> php variables
$GASPrice = strip_tags($_GET['GASPrice']);
$Ethanol = strip_tags($_GET['Ethanol']);
$MPGGAS = strip_tags($_GET['MPGGAS']);
$Tank = strip_tags($_GET['Tank']);
$MPGE85= strip_tags($_GET['MPGE85']);

// Comment comment comment!
// You need to place { and } on anything larger than 1 line after
// a if/else statement or you will recieve errors.
if ($MPGE85 <= 0)
{
	$E85Percent = ($MPGGAS / ($MPGGAS * .87));
}
else
{
        $E85Percent = ($MPGGAS / $MPGE85);
        $GASTank = ($GASPrice * $Tank);
        $E85 = ($Ethanol * 2) / 2;
        $E85TankNon = ($E85 * $Tank);
        $E85Tank = (($E85 * $Tank) * $E85Percent);
        $GasSaveTank = ($E85Tank - $GASTank);
        $GasSaveGall = ($E85Tank - $GASTank) / $Tank;
        $E85SaveTank = ($GASTank - $E85Tank);
        $E85SaveGall = ($GASTank - $E85Tank) / $Tank;
        $Diff = ($GASTank - $E85Tank);
        $SaveTankGas = "$" . round($GasSaveTank,2);
        $SaveGallGas = "$" . round($GasSaveGall,2);
        $SaveTankE85 = "$" . round($E85SaveTank,2);
        $SaveGallE85 = "$" . round($E85SaveGall,2);
        $E85Tank1 = "$" . round($E85Tank,2);
        $GASTank1 = "$" . round($GASTank,2);
        $E85TankNon1 = "$" . round($E85TankNon,2);
}

// Again you need { and } around each if and else statement
if ($Diff <= 0)
{
	echo '<table>
                 <tr>
                   <td colspan="2" align="center">Your best choice is using Gasoline.</td>
                 </tr>
                 <tr>
                   <td>Savings per tank</td>
                   <td>' . $SaveTankGas . '</td>
                 </tr>
                 <tr>
                   <td>Savings per gallon</td>
                   <td>' . $SaveGallGas . '</td>
              </table>';
}
else
{
        echo '<table>
                 <tr>
                   <td colspan="2" align="center">Your best choice is using E85.</td>
                 </tr>
                 <tr>
                   <td>Savings per tank</td>
                   <td>' . $SaveTankE85 . '</td>
                 </tr>
                 <tr>
                   <td>Savings per gallon</td>
                   <td>' . $SaveGallE85 . '</td>
                </tr>
             </table>
	     <table>
             <tr>
                <td>Cost to fill up with Gasoline</td>
                <td>' . $GASTank1 . '</td>
             </tr>
             <tr>
                 <td>Cost to fill up with E85</td>
                <td>' . $E85Tank1 . '</td>
             </tr>
             <tr>
                 <td>Cost to fill up with E85 *Adjusted Price</td>
                <td>' . $E85TankNon1 . '</td>
             </tr>
            </table>
<br />
* Adjusted Price : The adjusted price includes the extra cost due to loss in fuel mileage.';
}
?>
 
Last edited:

driveflexfuel

New Member
Messages
159
Reaction score
0
Points
0
I had to make a couple adjustments so that the code would operate properly and everything would display. Everything is still coming up as 0 and I cannot figure out why.
Code:
<?php
// This is the input from the form -> php variables
$GASPrice = strip_tags($_POST["GASPrice"]);
$Ethanol = strip_tags($_POST["Ethanol"]);
$MPGGAS = strip_tags($_POST["MPGGAS"]);
$Tank = strip_tags($_POST["Tank"]);
$MPGE85= strip_tags($_POST["MPGE85"]);

// Comment comment comment!
// You need to place { and } on anything larger than 1 line after
// a if/else statement or you will recieve errors.
if ($MPGE85 <= 0)
{
        $E85Percent = ($MPGGAS / ($MPGGAS * .87));
        $GASTank = ($GASPrice * $Tank);
        $E85 = ($Ethanol * 2) / 2;
        $E85TankNon = ($E85 * $Tank);
        $E85Tank = (($E85 * $Tank) * $E85Percent);
        $GasSaveTank = ($E85Tank - $GASTank);
        $GasSaveGall = ($E85Tank - $GASTank) / $Tank;
        $E85SaveTank = ($GASTank - $E85Tank);
        $E85SaveGall = ($GASTank - $E85Tank) / $Tank;
        $Diff = (GASTank - E85Tank);
        $SaveTankGas = "$" + round($GasSaveTank,2);
        $SaveGallGas = "$" + round($GasSaveGall,2);
        $SaveTankE85 = "$" + round($E85SaveTank,2);
        $SaveGallE85 = "$" + round($E85SaveGall,2);
        $E85Tank1 = "$" + round($E85Tank,2);
        $GASTank1 = "$" + round($GASTank,2);
        $E85TankNon1 = "$" + round($E85TankNon,2);
}
else
{
        $E85Percent = ($MPGGAS / $MPGE85);
        $GASTank = ($GASPrice * $Tank);
        $E85 = ($Ethanol * 2) / 2;
        $E85TankNon = ($E85 * $Tank);
        $E85Tank = (($E85 * $Tank) * $E85Percent);
        $GasSaveTank = ($E85Tank - $GASTank);
        $GasSaveGall = ($E85Tank - $GASTank) / $Tank;
        $E85SaveTank = ($GASTank - $E85Tank);
        $E85SaveGall = ($GASTank - $E85Tank) / $Tank;
        $Diff = (GASTank - E85Tank);
        $SaveTankGas = "$" + round($GasSaveTank,2);
        $SaveGallGas = "$" + round($GasSaveGall,2);
        $SaveTankE85 = "$" + round($E85SaveTank,2);
        $SaveGallE85 = "$" + round($E85SaveGall,2);
        $E85Tank1 = "$" + round($E85Tank,2);
        $GASTank1 = "$" + round($GASTank,2);
        $E85TankNon1 = "$" + round($E85TankNon,2);
}

// Again you need { and } around each if and else statement
if (Diff <= 0)
{
    echo "<table>
                 <tr>
                   <td colspan=2 align=center>Your best choice is using Gasoline.</td>
                 </tr>
                 <tr>
                   <td>Savings per tank</td>
                   <td>" . $SaveTankGas . "</td>
                 </tr>
                 <tr>
                   <td>Savings per gallon</td>
                   <td>" . $SaveGallGas . "</td>
              </table>
	<table>
             <tr>
                <td>Cost to fill up with Gasoline</td>
                <td>" . $GASTank1 . "</td>
             </tr>
             <tr>
                 <td>Cost to fill up with E85</td>
                <td>" . $E85Tank1 . "</td>
             </tr>
             <tr>
                 <td>Cost to fill up with E85 *Adjusted Price</td>
                <td>" . $E85TankNon1 . "</td>
             </tr>
            </table>
<br />
* Adjusted Price : The adjusted price includes the extra cost due to loss in fuel mileage.";
}
else
{
        echo "<table>
                 <tr>
                   <td colspan=2 align=center>Your best choice is using E85.</td>
                 </tr>
                 <tr>
                   <td>Savings per tank</td>
                   <td>" . $SaveTankE85 . "</td>
                 </tr>
                 <tr>
                   <td>Savings per gallon</td>
                   <td>" . $SaveGallE85 . "</td>
                </tr>
             </table>
         <table>
             <tr>
                <td>Cost to fill up with Gasoline</td>
                <td>" . $GASTank1 . "</td>
             </tr>
             <tr>
                 <td>Cost to fill up with E85</td>
                <td>" . $E85Tank1 . "</td>
             </tr>
             <tr>
                 <td>Cost to fill up with E85 *Adjusted Price</td>
                <td>" . $E85TankNon1 . "</td>
             </tr>
            </table>
<br />
* Adjusted Price : The adjusted price includes the extra cost due to loss in fuel mileage.";
}
?> 
[\code]
 
Last edited:

Jake

Developer
Contributors
Messages
4,057
Reaction score
5
Points
0
The error is because some of your math must be off... I'm not sure what your math is doing... since well its not commented... so I am not exactly sure what you are trying to get BUT! I know that on line 14 your math is off and is dividing by zero (like the error says) so you should check that and make sure the math is right.

Not trying to be mean or anything but i mean it would be much easier to read with more descriptive variable names... even if they are a bit longer. The variable $ethonal doesn't tell me if it's the price of ethonal, the amount of ethonal or what... just says ethonal :)
Edit:
You need to change "" + round(var) to

$E85TankNon1 = "$" . round($E85TankNon,2);
 
Last edited:

driveflexfuel

New Member
Messages
159
Reaction score
0
Points
0
/mileage loss when using ethanol
$E85Percent = ($MPGGAS / ($MPGGAS * .87));
//cost to fill with gasoline
$GASTank = ($GASPrice * $Tank);
//used due to error when programmed in javascript
$E85 = ($Ethanol * 2) / 2;
//cost to fill tank with ethanol
$E85TankNon = ($E85 * $Tank);
//cost to fill tank with ethanol with adjustment
$E85Tank = (($E85 * $Tank) * $E85Percent);
//savings if using gas is better per tank
$GasSaveTank = ($E85Tank - $GASTank);
//savings if using gas is better per gallon
$GasSaveGall = ($E85Tank - $GASTank) / $Tank;
//savings if using e85 is batter per tank
$E85SaveTank = ($GASTank - $E85Tank);
//savings if using e85 is better per gallon
$E85SaveGall = ($GASTank - $E85Tank) / $Tank;
//difference between prices
$Diff = (GASTank - E85Tank);
//rounding all calculations to nearest hundreth.
$SaveTankGas = "$" . round($GasSaveTank,2);
$SaveGallGas = "$" . round($GasSaveGall,2);
$SaveTankE85 = "$" . round($E85SaveTank,2);
$SaveGallE85 = "$" . round($E85SaveGall,2);
$E85Tank1 = "$" . round($E85Tank,2);
$GASTank1 = "$" . round($GASTank,2);
$E85TankNon1 = "$" . round($E85TankNon,2);

Warning: Division by zero in /home/e85and/public_html/TEST/savings/results.php on line 14

Warning: Division by zero in /home/e85and/public_html/TEST/savings/results.php on line 20

Warning: Division by zero in /home/e85and/public_html/TEST/savings/results.php on line 22
 
Last edited:

Jake

Developer
Contributors
Messages
4,057
Reaction score
5
Points
0
This line,

//difference between prices
$Diff = (GASTank - E85Tank);

does not have $'s on it's variables ...

and your if (Diff = something) well you need a $ in it too...

Check your Tank input on the form, make sure its posting "Tank" not "tank" or something. It seems like its coming in with 0 when it should be set by the user in the form.
 
Last edited:

natsuki

New Member
Messages
112
Reaction score
0
Points
0
make sure your variables for the divisor are not zero and if they are then give them a default value

is there any reason why the variable can become zero?
 

Jake

Developer
Contributors
Messages
4,057
Reaction score
5
Points
0
change your $_POST['']; to $_GET[''];

and don't leave spaces in file names, like form sample.php should be form_sample.php ... makes life easier
 
Last edited:

natsuki

New Member
Messages
112
Reaction score
0
Points
0
Your form goes to this url http://driveflexfuel.com/TEST/savings/results.php?http://driveflexfuel.com/TEST/savings/Form%20Sample.php which does not carry on the values in your form in sample.php, there's nothing

You get your values in the original page but not in the popup window. If you want them there then you'll need to put them one by one on the url through javascript. ><
 
Last edited:

Jake

Developer
Contributors
Messages
4,057
Reaction score
5
Points
0
When I did it they showed the right variables in the URL of the popup... but the script was using post instead of get...
 

natsuki

New Member
Messages
112
Reaction score
0
Points
0
That is after you've submitted once and the original url of the form becomes something like http://driveflexfuel.com/TEST/savings/Form%20Sample.php?GASPrice=3434234&Ethanol=32432432&Tank=324234&MPGGAS=34234324&MPGE85=0.00&submit=Calculate which is the previous submit. And when you submit again you'll see the values that are on the url of the form but not the ones on the form itself.

You are passing the data that is in the url of the form to the popup BEFORE the page actually reloads.
 
Last edited:

Jake

Developer
Contributors
Messages
4,057
Reaction score
5
Points
0
Yeah I see what he's doing right now, not a javascript nut so I'm not sure how you pass form variables from one page to a popup (hate popups lol)

Edit: I'd try to make it with a normal form and get rid of the javascript in the form... make sure it works then before you try to put it into a popup.
 
Last edited:

driveflexfuel

New Member
Messages
159
Reaction score
0
Points
0
I am fixing some things I was trying to do to many things at once.

I just fixed the form so that it pops up using a different script

Everything is working great and I thank all of you for the help.
 
Last edited:
Top