100 lines
3.3 KiB
PHP
100 lines
3.3 KiB
PHP
<?php
|
|
$defaultVolume1 = 40.5;
|
|
$defaultVolume2 = 35.7;
|
|
$defaultPrice = 1.499;
|
|
|
|
$readNumber = function (string $key, $default) {
|
|
if (!isset($_REQUEST[$key]) || $_REQUEST[$key] === '') {
|
|
return $default;
|
|
}
|
|
|
|
$raw = (string)$_REQUEST[$key];
|
|
$norm = str_replace([' ', ','], ['', '.'], $raw);
|
|
|
|
if (!is_numeric($norm)) {
|
|
return $default;
|
|
}
|
|
|
|
$val = (float)$norm;
|
|
|
|
if ($val < 0) {
|
|
$val = 0.0;
|
|
}
|
|
|
|
return $val;
|
|
};
|
|
|
|
$volume1 = $readNumber('volume1', $defaultVolume1);
|
|
$volume2 = $readNumber('volume2', $defaultVolume2);
|
|
$price = $readNumber('price', $defaultPrice);
|
|
|
|
$totalVolume = $volume1 + $volume2;
|
|
$costFill1 = $volume1 * $price;
|
|
$costFill2 = $volume2 * $price;
|
|
$totalCost = $totalVolume * $price;
|
|
|
|
$fmtCostFill1 = number_format($costFill1, 2, ',', '.');
|
|
$fmtCostFill2 = number_format($costFill2, 2, ',', '.');
|
|
$fmtTotalCost = number_format($totalCost, 2, ',', '.');
|
|
$fmtPrice = number_format($price, 3, ',', '.');
|
|
?>
|
|
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8" />
|
|
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
|
<title>Fuel Calculation (Task 3)</title>
|
|
<link rel="stylesheet" href="css/style.css" />
|
|
</head>
|
|
<body>
|
|
<h1>Fuel Calculation</h1>
|
|
<nav>
|
|
<p><a href="index.php">Home</a> | <a href="info.php">PHP Info</a></p>
|
|
</nav>
|
|
|
|
<form action="test.php" method="get" class="calc-form" style="margin-bottom:1rem; display:grid; gap:0.5rem; max-width: 420px;">
|
|
<label>
|
|
Fill 1 (L)
|
|
<input type="number" name="volume1" step="0.1" min="0" value="<?= htmlspecialchars((string)$volume1, ENT_QUOTES) ?>">
|
|
</label>
|
|
<label>
|
|
Fill 2 (L)
|
|
<input type="number" name="volume2" step="0.1" min="0" value="<?= htmlspecialchars((string)$volume2, ENT_QUOTES) ?>">
|
|
</label>
|
|
<label>
|
|
Price per liter (€)
|
|
<input type="number" name="price" step="0.001" min="0" value="<?= htmlspecialchars((string)$price, ENT_QUOTES) ?>">
|
|
</label>
|
|
<div style="display:flex; gap:0.5rem;">
|
|
<button type="submit">Calculate</button>
|
|
<a href="test.php" style="display:inline-block; padding:0.25rem 0.5rem; border:1px solid #ccc; border-radius:4px; text-decoration:none;">Reset</a>
|
|
</div>
|
|
<small>Note: Comma as decimal separator is allowed (e.g., 40,5).</small>
|
|
</form>
|
|
|
|
<p>Fill 1: <?= $volume1 ?> L</p>
|
|
<p>Fill 2: <?= $volume2 ?> L</p>
|
|
<p>Total volume: <?= $totalVolume ?> L</p>
|
|
|
|
<p>Price per liter: <?= $fmtPrice ?> €</p>
|
|
<p>Cost fill 1: <?= $fmtCostFill1 ?> €</p>
|
|
<p>Cost fill 2: <?= $fmtCostFill2 ?> €</p>
|
|
<p>Total cost: <strong><?= $fmtTotalCost ?> €</strong></p>
|
|
|
|
<table class="calc">
|
|
<thead>
|
|
<tr><th>Label</th><th>Value</th></tr>
|
|
</thead>
|
|
<tbody>
|
|
<tr><td>Fill 1 (L)</td><td><?= $volume1 ?></td></tr>
|
|
<tr><td>Fill 2 (L)</td><td><?= $volume2 ?></td></tr>
|
|
<tr><td>Total (L)</td><td><?= $totalVolume ?></td></tr>
|
|
<tr><td>Price per liter (€)</td><td><?= $fmtPrice ?></td></tr>
|
|
<tr><td>Cost fill 1 (€)</td><td><?= $fmtCostFill1 ?></td></tr>
|
|
<tr><td>Cost fill 2 (€)</td><td><?= $fmtCostFill2 ?></td></tr>
|
|
<tr><td>Total cost (€)</td><td><strong><?= $fmtTotalCost ?></strong></td></tr>
|
|
</tbody>
|
|
</table>
|
|
</body>
|
|
</html>
|