php - 编辑:在 PHP 中计算变量
问题描述
我是 PHP 的初学者,我被分配了创建 Gas Budget 计算器的任务。
我被困在如何添加数学运算并在提交后返回值。这是我在表单中的内容和链接的 PHP 文件之一。
当我按原样运行时,数据会存储在服务器上,但总数不会返回。感觉好像我已经很接近了,但我已经盯着这段代码看了 4 个小时,我的大脑很痛。请帮忙!
先感谢您!
HTML:
<?php
require_once 'functions.php';
$model = getViewModel();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Gas Budget Calculator</title>
<link rel="stylesheet" href="main.css">
</head>
<body>
<main>
<?php if($model->msg): ?>
<p class="error"><?=$model->msg?></p>
<?php endif; ?>
<form action="" method = "post">
<fieldset>
<legend>Gas Budget Calculator</legend>
<div class = "input-field">
<label for= "type" >Price of Gas:</label>
<select name = "type">
<option></option>
<?php foreach ($model->price as $value => $label): ?>
<option value="<?=$value?>"><?=$label?></option>
<? endforeach; ?>
</select>
</div>
<div class = "input-field">
<label for = "commute"> Average Commute Mileage:</label>
<input type="text" name = "commute" id = "commute">
</div>
<div class = "input-field">
<label for = "mpg"> Car Average Miles Per Gallon (MPG): </label>
<input type="text" name = "mpg" id = "mpg">
</div>
<div class="input-field">
<label>
<input type="checkbox" name="perks">Are you going to use fuel perks? (Check box if yes)
</label>
</div>
<div class = "input-field">
<label for = "groceries"> If yes, what is the average amount spent on groceries?</label>
<input type="text" name = "groceries" id = "groceries">
</div>
<div class="input-field">
<button type="submit">Submit</button>
</div>
</fieldset>
</form>
<?php if ($model->info): ?>
<table>
<tr>
<td>Price of Gas </td><td><?=$model->info->type?></td>
</tr>
<tr>
<td>Commute</td><td><?=$model->info->commute?></td>
</tr>
<tr>
<td>Miles Per Gallon</td><td><?=$model->info->mpg?></td>
</tr>
<tr>
<td>Fuel Perks?</td><td><?=$model->info->perksYesNo()?></td>
</tr>
<tr>
<td>Average Groceries</td><td><?=$model->info->groceryAverage()?></td>
</tr>
<tr>
<td>Total</td><td><?=$model->info->calculateTotal()?></td>
</tr>
</table>
<?php endif; ?>
</body>
</html>
PHP:
class viewModel {
public $info;
public $msg;
public $price = [
'87 octane' => 1.89,
'89 octane' => 1.99,
'92 octane' => 2.09
];
if(isset( $_POST['Submit']) {
if($msg!='')
}
class GasInfo {
private $isPerks;
public $type;
public $commute;
public $mpg;
public $groceries;
public $total;
public function __construct(int $type, int $commute, int $mpg, bool $perks, int $groceries, int $total){
$this ->type = $type;
$this ->commute = $commute;
$this ->mpg = $mpg;
$this ->perks = $perks;
$this ->groceries = $groceries;
$this ->total = $total;
}
public function isValid(): bool {
if (empty($this->commute)) {
return false;
}
if (empty($this->mpg)) {
return false;
}
return true;
}
function perksYesNo(): string {
return $this->isPerks ? 'Yes' : 'No';
}
function groceryAverage(int $groceries){
return floor( $groceries/100);
}
function calculateTotal (int $type, int $commute, int $mpg, bool $perks, int $groceries, int $total){
if ($perks == 'on'){
$total = ($commute / $mpg)*$type;
} else {
$total = ($commute / $mpg) * ($type - $groceries);
}
return $total;
echo "You spend $" + $total + " on gas every day.";
}
}
更多PHP:
<?php
require_once 'GasInfo.php';
function getViewModel(): ViewModel {
$model = new ViewModel();
}
return $model;
}
function handleSubmit() {
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
return null;
}
$type = $_POST['type'];
$commute = $_POST['commute'];
$mpg = $_POST['mpg'];
$isPerks = isset($_POST['perks']) ?? null;
$groceries = $_POST ['groceries'];
$total = $_POST('total');
$info = new GasInfo(
(int)$type,
(int)$commute,
(int)$mpg,
$isPerks,
(int)$groceries,
(int)$total
);
if (!$info->isValid()) {
throw new Exception("Please enter gas price, commute, and miles per gallon");
}
return $info;
}
解决方案
推荐阅读
- html - 引导模式中的数据选择器没有功能
- c - 创建密码,并出现函数指针错误已编辑!密码编译但重复大写
- php - 在数组 laravel excel 上调用成员函数 all()
- javascript - chart.js 动态调整垂直条之间的间隙
- c# - 无法将 Visual Studio 2019 与 MySQL 数据库连接并将文本框中的用户输入插入所述数据库(已解决)
- regex - 什么正则表达式会捕获从 second : mark 到行尾的所有内容?
- javascript - Ion RangeSlider 在 Angular 9 项目中没有正确重新加载
- laravel-nova - 如何更改 Laravel Nova 资源索引页上行的背景颜色?
- angular - 如何对angular7中具有@Input变量的组件进行单元测试?
- android - ViewPager2 在使用 FragmentStateAdapter 时不刷新 Fragment View