34959-vm/price_history.php
Flatlogic Bot e428ea4534 v1
2025-10-14 17:08:20 +00:00

100 lines
4.1 KiB
PHP

<?php
require_once __DIR__ . '/includes/header.php';
$pdo = db();
$message = '';
// Handle form submission for adding a new price
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['add_price'])) {
$fuel_type_id = $_POST['fuel_type_id'];
$price = trim($_POST['price']);
$date = $_POST['date'];
if (!empty($fuel_type_id) && !empty($price) && !empty($date)) {
try {
$stmt = $pdo->prepare("INSERT INTO price_history (fuel_type_id, price, date) VALUES (?, ?, ?)");
$stmt->execute([$fuel_type_id, $price, $date]);
$message = '<div class="alert alert-success">Price added successfully!</div>';
} catch (PDOException $e) {
$message = '<div class="alert alert-danger">Error: ' . $e->getMessage() . '</div>';
}
} else {
$message = '<div class="alert alert-warning">Please fill in all fields.</div>';
}
}
// Fetch related data for forms
$fuel_types = $pdo->query("SELECT id, name FROM fuel_types ORDER BY name")->fetchAll(PDO::FETCH_ASSOC);
// Fetch all price history with fuel type name
$stmt = $pdo->query("
SELECT ph.id, ph.price, ph.date, ft.name AS fuel_type_name
FROM price_history ph
JOIN fuel_types ft ON ph.fuel_type_id = ft.id
ORDER BY ph.date DESC, ft.name
");
$price_history = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>
<div class="row">
<div class="col-md-8">
<h2>Price History</h2>
<p>History of fuel prices.</p>
<table class="table table-striped table-hover">
<thead class="table-dark">
<tr>
<th>Date</th>
<th>Fuel Type</th>
<th>Price</th>
</tr>
</thead>
<tbody>
<?php if (empty($price_history)): ?>
<tr>
<td colspan="3" class="text-center">No price history found. Add one to get started.</td>
</tr>
<?php else: ?>
<?php foreach ($price_history as $price_entry): ?>
<tr>
<td><?php echo htmlspecialchars($price_entry['date']); ?></td>
<td><?php echo htmlspecialchars($price_entry['fuel_type_name']); ?></td>
<td><?php echo htmlspecialchars($price_entry['price']); ?></td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
</tbody>
</table>
</div>
<div class="col-md-4">
<div class="card">
<div class="card-body">
<h5 class="card-title">Add New Price</h5>
<?php echo $message; ?>
<form action="price_history.php" method="POST">
<div class="mb-3">
<label for="date" class="form-label">Date</label>
<input type="date" class="form-control" id="date" name="date" value="<?php echo date('Y-m-d'); ?>" required>
</div>
<div class="mb-3">
<label for="fuel_type_id" class="form-label">Fuel Type</label>
<select class="form-select" id="fuel_type_id" name="fuel_type_id" required>
<option value="">Select a Fuel Type</option>
<?php foreach ($fuel_types as $fuel_type): ?>
<option value="<?php echo $fuel_type['id']; ?>"><?php echo htmlspecialchars($fuel_type['name']); ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="mb-3">
<label for="price" class="form-label">Price</label>
<input type="number" step="0.01" class="form-control" id="price" name="price" required placeholder="e.g., 102.50">
</div>
<button type="submit" name="add_price" class="btn btn-primary">Add Price</button>
</form>
</div>
</div>
</div>
</div>
<?php require_once __DIR__ . '/includes/footer.php'; ?>