-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdashboard.php
More file actions
165 lines (144 loc) · 4.24 KB
/
dashboard.php
File metadata and controls
165 lines (144 loc) · 4.24 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
session_start();
include "db.php";
// Must be logged in
if (!isset($_SESSION['user'])) {
header("Location: login.php");
exit;
}
// Monthly filter
$monthCondition = "";
if (!empty($_GET['month'])) {
$month = (int) $_GET['month'];
$monthCondition = "AND MONTH(created_at) = $month";
}
// Totals
$income = mysqli_fetch_assoc(mysqli_query($conn,
"SELECT SUM(amount) AS total FROM transactions
WHERE type='income' $monthCondition"
))['total'] ?? 0;
$expense = mysqli_fetch_assoc(mysqli_query($conn,
"SELECT SUM(amount) AS total FROM transactions
WHERE type='expense' $monthCondition"
))['total'] ?? 0;
$balance = $income - $expense;
// Category-wise expense
$catResult = mysqli_query($conn,
"SELECT category, SUM(amount) AS total FROM transactions
WHERE type='expense' $monthCondition
GROUP BY category"
);
$categories = [];
$amounts = [];
while ($row = mysqli_fetch_assoc($catResult)) {
$categories[] = $row['category'];
$amounts[] = $row['total'];
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Dashboard</title>
<link rel="stylesheet" href="style.css">
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
</head>
<body>
<h2>Expense Dashboard</h2>
<div style="text-align:center;">
<a href="index.php" class="dashboard-btn">Add Transaction</a>
<a href="change_password.php" class="dashboard-btn">Change Password</a>
<a href="logout.php" class="dashboard-btn">Logout</a>
</div>
<!-- Month Filter -->
<form method="GET" style="text-align:center; margin-top:20px;">
<select name="month">
<option value="">All Months</option>
<?php for ($m = 1; $m <= 12; $m++): ?>
<option value="<?= $m ?>" <?= (!empty($_GET['month']) && $_GET['month'] == $m) ? 'selected' : '' ?>>
<?= date('F', mktime(0,0,0,$m,1)) ?>
</option>
<?php endfor; ?>
</select>
<button>Filter</button>
</form>
<!-- Summary Cards -->
<div class="cards">
<div class="card income">Income<br>₹<?= $income ?></div>
<div class="card expense">Expense<br>₹<?= $expense ?></div>
<div class="card balance">Balance<br>₹<?= $balance ?></div>
</div>
<!-- Charts -->
<div class="charts-row">
<div class="chart-box">
<h4>Expense by Category</h4>
<canvas id="expenseChart"></canvas>
</div>
<div class="chart-box">
<h4>Income vs Expense</h4>
<canvas id="summaryChart"></canvas>
</div>
</div>
<!-- Transactions Table -->
<div class="table-container">
<table>
<tr>
<th>Type</th>
<th>Category</th>
<th>Amount</th>
<th>Date</th>
<th>Action</th>
</tr>
<?php
$result = mysqli_query($conn,
"SELECT * FROM transactions
WHERE 1=1 $monthCondition
ORDER BY created_at DESC"
);
while ($row = mysqli_fetch_assoc($result)):
?>
<tr>
<td><?= ucfirst($row['type']) ?></td>
<td><?= $row['category'] ?></td>
<td>₹<?= $row['amount'] ?></td>
<td><?= $row['created_at'] ?></td>
<td>
<a href="edit.php?id=<?= $row['id'] ?>">Edit</a> |
<a href="delete.php?id=<?= $row['id'] ?>"
onclick="return confirm('Delete this transaction?')">
Delete
</a>
</td>
</tr>
<?php endwhile; ?>
</table>
</div>
<!-- Charts Script -->
<script>
new Chart(document.getElementById('expenseChart'), {
type: 'doughnut',
data: {
labels: <?= json_encode($categories) ?>,
datasets: [{
data: <?= json_encode($amounts) ?>,
borderWidth: 1
}]
}
});
new Chart(document.getElementById('summaryChart'), {
type: 'bar',
data: {
labels: ['Income', 'Expense', 'Balance'],
datasets: [{
data: [<?= $income ?>, <?= $expense ?>, <?= $balance ?>],
borderWidth: 1
}]
},
options: {
scales: {
y: { beginAtZero: true }
}
}
});
</script>
</body>
</html>