The following files exists in this folder. Click to view.
bank.php134 lines UTF-8 Unix (LF) 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
<?php
# Variabler
$GLOBALS['filename'] = "transactions.txt";
$balance = 0;
$transactions = array();
$transaction_output = "";
# Funktioner
# Funktionen reload() laddar om sidan
function reload($mess=""){
header("location: ?mess=$mess");
exit();
}
# Funktionen format_amount() formaterar tal vid utskrift
function format_amount($amount){
$output = number_format($amount, 0, ",", " ")."kr";
if ($amount < 0) {
$output = "<span class='text-red'>{$output}</span>";
}
return($output);
}
# Funktionen transaction() skriver en transaktion till filen
function transaction($amount, $br=true){
$date = date('Y-m-d H:i:s');
$file = fopen($GLOBALS['filename'], 'a');
$br = $br ? "\n" : "";
fwrite($file, "$br$amount|$date");
fclose($file);
}
# Lagrar meddelande om det finns
$mess = isset($_GET['mess']) && $_GET['mess'] != "" ? "<span class='text-white'>{$_GET['mess']}</span>" : "";
# Tar bort filen
if(isset($_POST['delete_account'])){
unlink($GLOBALS['filename']);
reload('Det gamla kontot har tagits bort och ett nytt konto har skapats');
}
# File Maker
if(!file_exists($GLOBALS['filename'])){
transaction(1000, false);
}
# Transaction Reader
$file = fopen($GLOBALS['filename'], "r");
while(!feof($file)){
$temp = fgets($file);
if(!empty($temp)){
$transactions[] = $temp;
}
}
# Loopar igenom alla transaktioner & bygger tabell
$transaction_output = "<table class='min-w-full bg-white shadow-md rounded-lg overflow-hidden'><thead class='bg-gray-200'><tr><th class='py-2 px-4'>Nr</th><th class='py-2 px-4'>Belopp</th><th class='py-2 px-4'>Datum</th><th class='py-2 px-4'>Saldo</th></tr></thead><tbody>";
$i = 1;
foreach($transactions as $t){
list($t, $d) = explode("|", $t);
$balance += $t;
$t = format_amount($t);
$b = format_amount($balance);
$transaction_output .= "\n<tr><td class='py-2 px-4 border-t border-gray-300'>{$i}</td><td class='py-2 px-4 border-t border-gray-300'>{$t}</td><td class='py-2 px-4 border-t border-gray-300'>{$d}</td><td class='py-2 px-4 border-t border-gray-300'>{$b}</td></tr>";
$i++;
}
$transaction_output .= "</tbody></table>";
# Bank Manager
if(isset($_POST["amount"])){
$file = fopen($GLOBALS['filename'], "a");
if($_POST["action"] == "deposit"){
$amount = $_POST["amount"];
} else {
if($_POST["amount"] > $balance){
reload('Uttaget är större än saldo och kan ej genomföras');
} else {
$amount = -$_POST["amount"];
}
}
transaction($amount);
reload();
}
?>
<!DOCTYPE html>
<html lang="sv">
<head>
<meta charset="UTF-8">
<title>Bank</title>
<script src="https://cdn.tailwindcss.com"></script>
</head>
<body class="bg-red-950">
<div class="max-w-4xl mx-auto p-6">
<div class="text-center mb-4">
<h1 class="text-3xl font-bold text-white">Banken</h1>
<?php echo $mess; ?>
</div>
<main class="flex justify-between">
<div class="bg-red-700 shadow-md rounded-lg p-4 w-1/3 mr-4">
<h3 class="text-xl font-bold text-white">Insättning / Uttag</h3>
<form action="?" method="POST" class="my-4">
<label for="belopp" class="block mb-2 text-white">Belopp:</label>
<input type="number" min="1" name="amount" placeholder="belopp" required class="w-full border rounded p-2 mb-4" />
<div class="mb-4">
<input type="radio" value="deposit" name="action" checked>
<label class="ml-2 text-white">Insättning</label>
</div>
<div class="mb-4">
<input type="radio" value="withdraw" name="action">
<label class="ml-2 text-white">Uttag</label>
</div>
<input type="submit" class="bg-red-800 text-white py-2 px-4 rounded" name="submit" value="Utför">
</form>
<form action="?" method="POST">
<input type="submit" class="bg-red-800 text-white py-2 px-4 rounded" name="delete_account" value="Ta bort konto">
</form>
</div>
<div class="bg-red-700 shadow-md rounded-lg p-4 w-2/3">
<h3 class="text-xl font-bold text-white">Saldo</h3>
<?php echo "<p class='text-2xl font-bold text-white'>".format_amount($balance)."</p>"; ?>
<h3 class="text-xl font-semibold mt-4 text-white">Transaktioner</h3>
<?php echo $transaction_output; ?>
</div>
</main>
</div>
</body>
</html>