Simple calculations in PHP

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Cowbie
    New Member
    • Nov 2007
    • 5

    Simple calculations in PHP

    Hello everyone,

    I'm very new to PHP and I'm keen to learn how it all works. I've been looking for help all day and reading tutorials etc which is how I came accross this forum - so here's hoping for some answers :)

    First off I'll tell you what I'm doing. I'm making a calculator of some form which will do some simple calculations on the numbers entered into a form. For example, adding, multiplying etc. At the moment just to get me going I want to add two numbers together which would have been entered by the user. The calculator itself will be used to estimate the top speed of a car depending on what's been entered (doesn't have to be accurate of course, it's by no means professional or anything like that it's just college work).

    I may have made some silly mistakes because of how new all this is to me, but here is my code:
    [code=html]
    <html>
    <head>
    <title>Form :: Results</title>
    </head>

    <body>
    <h1>Top Speed Calulator</h1>
    <img src="gtx.jpg" alt="My VW Scirocco GTX">
    <br>
    <br>
    Here are the options you submitted:
    <br>
    <br>

    <table border="3" bgcolor=#FFFF99 >
    <tr>
    <th>Property</th>
    <th>Value</th>
    </tr>

    <tr>
    <td>Chassis Weight (lbs)</td>
    <td><?php echo number_format($ _POST['chassis']); ?></td>
    </tr>

    <tr>
    <td>Brake Horse Power (BHP)</td>
    <td><?php echo number_format($ _POST['bhp']); ?></td>
    </tr>

    <tr>
    <td>Number of Doors (3\5)</td>
    <td><?php echo htmlspecialchar s($_POST['doors']); ?></td>
    </tr>

    <tr>
    <td>Dropped Suspension? Lowered by... (mm)</td>
    <td><?php echo htmlspecialchar s($_POST['drop']); ?></td>
    </tr>

    <tr>
    <td>Engine Modifications</td>
    <td><?php echo htmlspecialchar s($_POST['mods']); ?></td>
    </tr>

    <tr>
    <td>Fuel Feed Type</td>
    <td><?php echo htmlspecialchar s($_POST['fuelfeed']); ?></td>
    </tr>

    <tr>
    <td>Your email address (for our newsletter)</td>
    <td><?php echo htmlspecialchar s($_POST['email']); ?></td>
    </tr>
    </table>

    <?php $speed = $chassis + $bhp;?>
    <?php echo number_format($ speed); ?>

    <br>
    </body>
    </html>
    [/code]

    As you can probably see, right now it only shows you what the user has entered on the form on the previous page, but the code at the bottom is where I'm trying to get the calculations to work - right now adding the chassis weight and the bhp values together.

    Can anyone shed some light on this for me?

    Many thanks

    PS - I can give you the code for the form itself if that's of any help to anyone
    Last edited by Atli; Nov 5 '07, 09:11 PM. Reason: Added [code] tags.
  • Motoma
    Recognized Expert Specialist
    • Jan 2007
    • 3236

    #2
    You will first need to parse through your expression and transform it into a Parse Tree or RPN . Afterwards, analyze the elements and operators and perform the appropriate operations on the running total.

    Comment

    • Cowbie
      New Member
      • Nov 2007
      • 5

      #3
      Thanks for those links. I think I didn't quite explain myself properly in my original post. The problem I *think* I'm having is the code I've written:

      <?php $speed = $chassis + $bhp;?>
      <?php echo number_format($ speed); ?>

      It always seems to return 0 rather than any other number. Why is this? Can anyone tinker with my code or hand me some pointers?
      You can look at the actual website here

      I apologise for not having a good knowledge of all this and if those links will help me a great deal more than they have so far, I'm just rather confused right now which is why I'm posting up here. Usually, I don't post on forums regarding issues like this as I can mostly sort them using tutorials elsewhere - but this has me around the bend!

      Comment

      • Motoma
        Recognized Expert Specialist
        • Jan 2007
        • 3236

        #4
        Originally posted by Cowbie
        Thanks for those links. I think I didn't quite explain myself properly in my original post. The problem I *think* I'm having is the code I've written:

        <?php $speed = $chassis + $bhp;?>
        <?php echo number_format($ speed); ?>

        It always seems to return 0 rather than any other number. Why is this? Can anyone tinker with my code or hand me some pointers?
        You can look at the actual website here
        Okay, I understand your problem a little better now.
        Your issue is that you are using $chassis and $bhp without ever assigning values to them.

        Comment

        • Cowbie
          New Member
          • Nov 2007
          • 5

          #5
          Originally posted by Motoma
          Okay, I understand your problem a little better now.
          Your issue is that you are using $chassis and $bhp without ever assigning values to them.
          Ok, that helps me a great deal, thanks!
          However, I thought it would have taken the values from the ones shown in the table - for example $_POST['chassis']

          How can I fix this, or assign them the values given in the table?


          EDIT:
          Ok, I think I have it figured thanks to your post. I did:

          <?php $chassis = $_POST['chassis'] ?>
          <?php $bhp = $_POST['bhp'] ?>

          <?php $speed = $chassis + $bhp;?>
          <?php echo number_format($ speed); ?>

          It now seems to be working fine. Great thanks!
          However, I'm wondering if this was this the correct way to fix the problem?

          Comment

          • Motoma
            Recognized Expert Specialist
            • Jan 2007
            • 3236

            #6
            Originally posted by Cowbie
            Ok, that helps me a great deal, thanks!
            However, I thought it would have taken the values from the ones shown in the table - for example $_POST['chassis']

            How can I fix this, or assign them the values given in the table?


            EDIT:
            Ok, I think I have it figured thanks to your post. I did:

            <?php $chassis = $_POST['chassis'] ?>
            <?php $bhp = $_POST['bhp'] ?>

            <?php $speed = $chassis + $bhp;?>
            <?php echo number_format($ speed); ?>

            It now seems to be working fine. Great thanks!
            However, I'm wondering if this was this the correct way to fix the problem?

            Yes, this was a perfectly acceptable way to solve the problem, though elegant it is not.

            A nicer way would be:

            [php]
            <?php
            $speed = $_POST['chassis'] + $_POST['bhp'];
            echo number_format($ speed);
            ?>
            [/php]

            or even

            [php]
            <?php
            echo number_format($ _POST['chassis'] + $_POST['bhp']);
            ?>
            [/php]

            Comment

            Working...