mercredi 2 juin 2021

Calculating Depreciation using php

this is my first time asking on here as a beginner at coding so forgive me if my questions seem like basic knowledge I'm still learning :) so I was trying to recreate this form and output using html and php: this is the screenshot of the code I was trying to recreate

and I managed to successfully produce the html and php (I'll add them for reference) however there is something in the calculation that is not exactly correct my html code:

    <!DOCTYPE html>
<html>
    <head>
        <title>Car Depreciation</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
        <form action="depreciation.php">
            <table>
                <tr>
                    <td>Original Price</td>
                    <td><input type="text" name="price" size="5"> Dollars</td>
                </tr>
                <tr>
                    <td>Residual Value</td>
                    <td><input type="text" name="residual" size="5"> Dollars</td>
                </tr>
                <tr><td><input type="submit"></td></tr>
            </table>
        </form>
    </body>
</html>

and my php code is:

    <!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <table border="1" cellspacing="0" cellpadding="5">
            <tr>
                <th>Year</th>
                <th>Value at<br>beginning</th>
                <th>Annual<br>Depreciation</th>
                <th>Accumulated<br>Depreciation</th>
                <th>Value at<br>end</th>
            </tr>
            <?php
            //Declaration of variables
            $price = $_GET["price"];
            $residual = $_GET["residual"];
            $accumulateddep = 0;
            //Calculations, Loops and Printing
            for ($year = 1; $year <= 5; $year++) {
                $annualdep = ($price - $residual) / 5;
                $accumulateddep+=$annualdep;
                $begvalue=$price-$accumulateddep;
                $endvalue = $begvalue - $annualdep;
                if ($year % 2 == 0)
                    echo "<tr>
                    <td>$year</td>
                    <td>$begvalue</td>
                    <td>$annualdep</td>
                    <td>$accumulateddep</td>
                    <td>$endvalue</td>
                    </tr>";
                else
                    echo "<tr style='background-color:lightgrey'>
                    <td>$year</td>
                    <td>$begvalue</td>
                    <td>$annualdep</td>
                    <td>$accumulateddep</td>
                    <td>$endvalue</td>
                    </tr>";
            }
            ?>
        </table>
    </body>
</html>

my issue is that when I test it out, I want the first year's calculation to use the entered value and then move on from there however what my code did was calculate depreciation for the first year then used that end value to continue (will insert a screenshot of my output for reference) screenshot of my output using my codes what needs to be modified in order to accurately fix the depreciation calculation for the first year? (quick update: someone mentioned I should state my example values in text so here they are: I use 17000 for the original price and 0.04 as the depreciation rate so the first year's calculation should be: 17000-3000=14000; 3000 is the annual depreciation according to the formula used to calculate it; the second year should use the end value of the first year so 14000-3000=11000 and so on for the five years) thank you in advance :)

Aucun commentaire:

Enregistrer un commentaire