+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 17

Thread: PHP Calculations

  1. #1
    driveflexfuel is offline x10 Sophmore driveflexfuel is an unknown quantity at this point
    Join Date
    Jul 2008
    Posts
    159

    PHP Calculations

    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 by driveflexfuel; 10-28-2008 at 01:18 PM.

  2. #2
    Jake's Avatar
    Jake is offline Developer Jake is an unknown quantity at this point
    Join Date
    Apr 2005
    Location
    Winona, MN
    Posts
    2,084

    Re: PHP Calculations

    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 Code:
    <?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']);
    $MPGE85strip_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 by Jake; 10-28-2008 at 02:23 PM. Reason: Updated for latest changes
    Jake Omann | Developer
    █ 888-X10-9668 - jake[@]x10hosting.com
    x10Hosting - Giving Away Hosting Since 2004
    Premium Hosting | VPS Services

  3. #3
    driveflexfuel is offline x10 Sophmore driveflexfuel is an unknown quantity at this point
    Join Date
    Jul 2008
    Posts
    159

    Re: PHP Calculations

    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 by driveflexfuel; 10-28-2008 at 01:55 PM.

  4. #4
    Jake's Avatar
    Jake is offline Developer Jake is an unknown quantity at this point
    Join Date
    Apr 2005
    Location
    Winona, MN
    Posts
    2,084

    Re: PHP Calculations

    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 by Jake; 10-28-2008 at 02:01 PM. Reason: Automerged Doublepost
    Jake Omann | Developer
    █ 888-X10-9668 - jake[@]x10hosting.com
    x10Hosting - Giving Away Hosting Since 2004
    Premium Hosting | VPS Services

  5. #5
    driveflexfuel is offline x10 Sophmore driveflexfuel is an unknown quantity at this point
    Join Date
    Jul 2008
    Posts
    159

    Re: PHP Calculations

    /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 by driveflexfuel; 10-28-2008 at 02:08 PM.

  6. #6
    Jake's Avatar
    Jake is offline Developer Jake is an unknown quantity at this point
    Join Date
    Apr 2005
    Location
    Winona, MN
    Posts
    2,084

    Re: PHP Calculations

    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 by Jake; 10-28-2008 at 02:17 PM. Reason: added
    Jake Omann | Developer
    █ 888-X10-9668 - jake[@]x10hosting.com
    x10Hosting - Giving Away Hosting Since 2004
    Premium Hosting | VPS Services

  7. #7
    natsuki's Avatar
    natsuki is offline x10 Sophmore natsuki is an unknown quantity at this point
    Join Date
    Sep 2008
    Posts
    112

    Re: PHP Calculations

    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?

  8. #8
    driveflexfuel is offline x10 Sophmore driveflexfuel is an unknown quantity at this point
    Join Date
    Jul 2008
    Posts
    159

    Re: PHP Calculations

    I am still receiving errors you can see the form here.

    http://driveflexfuel.com/TEST/savings/Form Sample.php

    Im not sure how I should complete the calculation to that they =0 if they are negative.
    Last edited by driveflexfuel; 10-28-2008 at 02:21 PM.

  9. #9
    Jake's Avatar
    Jake is offline Developer Jake is an unknown quantity at this point
    Join Date
    Apr 2005
    Location
    Winona, MN
    Posts
    2,084

    Re: PHP Calculations

    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 by Jake; 10-28-2008 at 02:24 PM.
    Jake Omann | Developer
    █ 888-X10-9668 - jake[@]x10hosting.com
    x10Hosting - Giving Away Hosting Since 2004
    Premium Hosting | VPS Services

  10. #10
    driveflexfuel is offline x10 Sophmore driveflexfuel is an unknown quantity at this point
    Join Date
    Jul 2008
    Posts
    159

    Re: PHP Calculations

    Ill post again when I can test everything for some reason my server is down again.

+ Reply to Thread
Page 1 of 2 12 LastLast

Similar Threads

  1. Ever Been Suspended For Using PHP?
    By dragoneye_xp in forum Off Topic
    Replies: 26
    Last Post: 08-16-2009, 07:17 PM
  2. [PHP] Variables in PHP
    By Bryon in forum Tutorials
    Replies: 15
    Last Post: 01-29-2009, 09:46 AM
  3. currently have an application pending php
    By biomasti in forum Free Hosting
    Replies: 1
    Last Post: 09-03-2008, 01:58 PM
  4. php errors galore
    By DMG Online in forum Scripts & 3rd Party Apps
    Replies: 9
    Last Post: 05-17-2008, 06:23 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers