View sourcecode

The following files exists in this folder. Click to view.

bank.php

134 lines UTF-8 Unix (LF)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?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($amount0","" ")."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(1000false);
}

# 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>