php - 我无法根据用户输入的初始值计算增量值并将其显示在表格中
问题描述
我正在尝试构建一个折旧计算器,以表格形式显示价值、使用年份和剩余价值,如下所示:地垫的购买价格 = 10.00 美元
(数据输入第一页)折旧年数 = 5 残值 = 0 项目:地垫成本:$10.00
(第二页输出)
年折旧至今剩余价值
1 2.00 8.00
2 4.00 6.00
我的问题是,我什至无法理解如何在每次增加折旧后重新计算它们时同时更新折旧和剩余价值变量。html页面没有问题,只是php页面给我带来了麻烦。我能摆脱的最好的是错误:解析错误:语法错误,意外变量“$ i”,期待“,”或“; " 在第 65 行的 C:\xampp\htdocs\deprecalc.php 中。下面列出了我构建这两个页面的尝试(首先是接受输入的文件,然后是输出结果的文件:
<!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> </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>
解决方案
修复您提到的错误非常容易。让我们看一下第 114 行:
echo "<td>"$i++"</td>";
在这里,您将连接 HTML 标记<td>
和您的计数器值$i
。要在 PHP 中连接,您需要使用句点 [.] 字符,如下所示:
echo "<td>" . $i++ . "</td>";
解决此问题后,您还需要在第 120 行和第 128 行使用句点。
此处列出的行号表示代码在您粘贴到问题中的两个组合文件中的位置。
推荐阅读
- android - Android:将框架添加到活动时出错
- jquery - 元素内“选定”或“选定”子项的可排序包含
- mysql - 如何反转或过滤掉在其他部门从事项目的两名员工?
- python-3.x - 从字典中检索某个值
- python - 在python中获取“字母”下一个unicode字符
- python - 我试图了解链表是如何工作的
- java - 使用 while 循环将产品添加到对象
- python-2.7 - 无法让 import gevent 与我的 c++ python 应用程序一起使用
- javascript - 有没有办法显示网站上托管的抽搐频道?
- javascript - 如何将一个将默认图像设置为参数的函数传递给正在获取图像 src 的 .attr()?