Lehe kood: json_php.php
<?php
$jsonFile = "data/trips.json";
$data = [];
// --- Näita selle lehe koodi ---
if (isset($_GET["showcode"])) {
echo "<h1 style='text-align:center;'>Lehe kood: json_php.php</h1>";
echo "<div style='margin:20px auto;max-width:1000px;background:#f8f8f8;padding:15px;border-radius:10px;box-shadow:0 0 5px rgba(0,0,0,0.1);overflow:auto;'>";
highlight_file(__FILE__);
echo "</div>";
echo "<p style='text-align:center;'><a href='json_php.php' class='btn'>⬅ Tagasi lehele</a></p>";
exit;
}
// --- Lae JSON ---
if (file_exists($jsonFile)) {
$json = file_get_contents($jsonFile);
$decoded = json_decode($json, true);
$data = $decoded["trip"] ?? [];
}
// --- Kustutamine ---
if (isset($_GET["delete"])) {
$id = $_GET["delete"];
$data = array_filter($data, fn($t) => ($t["@attributes"]["id"] ?? null) != $id);
file_put_contents(
$jsonFile,
json_encode(["trip" => array_values($data)], JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE)
);
header("Location: json_php.php");
exit;
}
// --- Otsing ---
$search = $_GET["q"] ?? "";
if ($search) {
$low = mb_strtolower($search);
$data = array_filter($data, fn($t) =>
str_contains(mb_strtolower($t["destination"] ?? ""), $low) ||
str_contains(mb_strtolower($t["note"] ?? ""), $low)
);
}
?>
<!DOCTYPE html>
<html lang="et">
<head>
<meta charset="UTF-8">
<title>Reisid (PHP)</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<h1>Reiside nimekiri (PHP)</h1>
<form method="get" style="text-align:center;">
<input type="text" name="q" placeholder="Otsi sihtkoht..." value="<?= htmlspecialchars($search) ?>">
<button type="submit" class="btn">Otsi</button>
<a href="json_php.php" class="btn">Tühjenda</a>
</form>
<div style="text-align:center;margin-top:15px;">
<a href="json_add.php" class="btn green">Lisa uus reis</a>
</div>
<table>
<tr>
<th>ID</th><th>Sihtkoht</th><th>Algus</th><th>Lõpp</th><th>Hinnang</th><th>Märkus</th><th>Kogumaksumus</th><th></th>
</tr>
<?php if (empty($data)): ?>
<tr><td colspan="8" style="text-align:center;">Andmed puuduvad</td></tr>
<?php else: ?>
<?php foreach ($data as $trip): ?>
<tr>
<td><?= htmlspecialchars($trip["@attributes"]["id"] ?? "") ?></td>
<td><?= htmlspecialchars($trip["destination"] ?? "") ?></td>
<td><?= htmlspecialchars($trip["dates"]["start"] ?? "") ?></td>
<td><?= htmlspecialchars($trip["dates"]["end"] ?? "") ?></td>
<td><?= htmlspecialchars($trip["rating"] ?? "") ?></td>
<td><?= htmlspecialchars($trip["note"] ?? "") ?></td>
<td><?= htmlspecialchars($trip["totalCost"] ?? "") ?></td>
<td>
<?php if (!empty($trip["@attributes"]["id"])): ?>
<a href="?delete=<?= htmlspecialchars($trip["@attributes"]["id"]) ?>" class="btn red small">X</a>
<?php endif; ?>
</td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
</table>
<!-- Nupp koodi kuvamiseks (all, "под код") -->
<p style="text-align:center;margin-top:20px;">
<a href="?showcode=1" class="btn">Näita PHP koodi</a>
</p>
</body>
</html>
⬅ Tagasi lehele