0

I'm trying to build a depreciation calculator that displays the value, year of use, and remaining value in table form like so: Purchase price of the Floor Pads = $10.00

(data entered in the first page) No. of years to depreciate = 5 Salvage Value = 0 Item:Floor Pads Cost:$10.00

(output on the second page)

Year Depreciation To Date Remaining Value

1 2.00 8.00

2 4.00 6.00

My issue is that I can't even begin to understand how I would update both the depreciation and the remaining value variables at the same time every time I recalculate them after they're incremented. There's nothing wrong with the html page, it's just the php page that's giving me trouble.The best I can get out of it is the error: Parse error: syntax error, unexpected variable "$i", expecting "," or ";" in C:\xampp\htdocs\deprecalc.php on line 65. My attempt at building these two pages is listed below (first the file which accepts the input, and then the file which outputs the results:

<!DOCTYPE html> 
<html>
<head>
    
<title>Depreciation Calculator</title>
    <link rel="stylesheet" type="text/css" href="main.css"/>
    
    
</head>
<body>
    
    
<div id="content">
    <h1>Enter Your info</h1>
    <form action="deprecalc.php" method="post">
    <div id="data">
   
        
    <label>Item name</label>
    <input type="text" name="item_name"/>
        </br>    
    <label>Purchase price</label>
    <input type="text" name="purchase_price"/>
        </br>
    <label>No. of years</label>
    <input type="text" name="no_of_years"/>
        </br>
    <label>Salvage value</label>
    <input type="text" name="salvage_value"/>
        </br>
   
    
    </div>
        <div id="buttons">
    <label>&nbsp</label>
        <input type="submit" value="Calculate"></br>
        </div>    
    </form>
</div>    
    
    
</body>

</html>


//Here is the file that outputs the results

<?php
$item_name = $_POST['item_name'];
$purchase_price = $_POST['purchase_price'];
$no_of_years = $_POST['no_of_years'];
$salvage_value = $_POST['salvage_value'];
$depreciation = ($purchase_price - $remaining_value) / $no_of_years;
$remaining_value = $purchase_price - $depreciation; 




?>


<!DOCTYPE html> 
<html>
<head>
<title>Depreciation Calculator</title>
    <link rel="stylesheet" type="text/css" href="main.css"/>
</head>
<body>
<div id="content">

<style>
#customers {
  font-family: Arial, Helvetica, sans-serif;
  border-collapse: collapse;
  width: 100%;
}

#customers td, #customers th {
  border: 1px solid #ddd;
  padding: 8px;
}

#customers tr:nth-child(even){background-color: #f2f2f2;}

#customers tr:hover {background-color: #ddd;}

#customers th {
  padding-top: 12px;
  padding-bottom: 12px;
  text-align: left;
  background-color: navy;
  color: white;
}
</style>
</head>
<body>

    <h2>Item: <?php echo $item_name;?></h2></br>
    <h2>Cost: <?php echo $purchase_price;?></h2></br>
    
<table id="customers">
  <tr>
    <th>Year</th>
    <th>Depreciation to date</th>
    <th>Remaining value</th>
  </tr>
  <tr>
    <td><?php
        $i=1;
        
        while ($i <= $no_of_years){
            echo "<td>"$i++"</td>";
        }
    
        ?></td>
    <td><?php
        
        while ($remaining_value >= 0){
            echo "<td>"$depreciation "</td>";
        }
        
        ?></td>
    <td><?php
      
        
        while ($depreciation <= $purchase_price){
            echo "<td>"$remaining_value"</td>";
        }
       
        ?></td>
  </tr>
</table>

</body>
</html>

   
</div>    
</body>
</html>
wno7736
  • 1
  • 2

1 Answers1

0

To fix the error you mentioned is pretty easy. Let's look at line 114:

echo "<td>"$i++"</td>";

Here you're concatenating HTML tags <td> and your counter value $i. To concatenate in PHP you will need to use the period [.] character like so:

echo "<td>" . $i++ . "</td>";

Once this is fixed, you'll need to use the period for lines 120 and 128 as well.

The line numbers listed here represent the code's position in the two combined files that you pasted in your question.

Rowan
  • 1
  • 1