Project: Personal Expense Tracker (PHP & MySQLi)
এই প্রজেক্টটি আপনার ব্যয় ও আয় ট্র্যাক করতে সাহায্য করবে। এটি PHP এবং MySQLi ব্যবহার করে তৈরি করা হয়েছে।
১. ডেটাবেজ সেটআপ
Database Name: expense_tracker
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(100),
amount DECIMAL(10,2),
type ENUM(‘Income’, ‘Expense’),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
ব্যাখ্যা: আমরা একটি `expenses` টেবিল বানিয়েছি যেখানে ব্যয়ের শিরোনাম, পরিমাণ ও টাইপ (Income বা Expense) রাখা হবে।
২. ডেটাবেজ কানেকশন (db.php)
$host = ‘localhost’;
$user = ‘root’;
$password = ”;
$db = ‘expense_tracker’;
$conn = new mysqli($host, $user, $password, $db);
if ($conn->connect_error) {
die(“Connection failed: ” . $conn->connect_error);
}
?>
ব্যাখ্যা: এটি ডেটাবেজ কানেকশন তৈরি করার জন্য ব্যবহার করা হয়।
৩. ইনপুট ফর্ম (add_expense.php)
<input type=”text” name=”title” placeholder=”Title” required><br><br>
<input type=”number” step=”0.01″ name=”amount” placeholder=”Amount” required><br><br>
<select name=”type”>
<option value=”Income”>Income</option>
<option value=”Expense”>Expense</option>
</select><br><br>
<input type=”submit” value=”Add Entry”>
</form>
ব্যাখ্যা: ব্যবহারকারী এখানে শিরোনাম, পরিমাণ এবং টাইপ ইনপুট দিতে পারবে।
৪. ডেটা সংরক্ষণ (save.php)
include ‘db.php’;
$title = $_POST[‘title’];
$amount = $_POST[‘amount’];
$type = $_POST[‘type’];
$sql = “INSERT INTO expenses (title, amount, type) VALUES (‘$title’, ‘$amount’, ‘$type’)”;
$conn->query($sql);
header(“Location: list.php”);
?>
ব্যাখ্যা: ফর্ম থেকে পাওয়া ইনপুট ডেটাবেজে ইনসার্ট করে list.php ফাইলে পাঠানো হচ্ছে।
৫. রিপোর্ট দেখানো (list.php)
include ‘db.php’;
$result = $conn->query(“SELECT * FROM expenses ORDER BY created_at DESC”);
echo “<table style=’width:100%; border-collapse: collapse; border:1px solid #ccc;’>”;
echo “<tr style=’background:#3498db; color:white;’><th>Title</th><th>Amount</th><th>Type</th><th>Date</th></tr>”;
while($row = $result->fetch_assoc()) {
echo “<tr><td style=’padding:10px;’>”.$row[‘title’].”</td><td>”.$row[‘amount’].”</td><td>”.$row[‘type’].”</td><td>”.$row[‘created_at’].”</td></tr>”;
}
echo “</table>”;
?>
ব্যাখ্যা: এখানে ইউজারদের সব ইনপুট করা ইনকাম ও এক্সপেন্স লিস্ট আকারে দেখানো হচ্ছে।
৬. আউটপুট
Output Example:
| Title | Amount | Type | Date |
|---|---|---|---|
| Salary | $500.00 | Income | 2025-05-13 |
| Groceries | $50.00 | Expense | 2025-05-13 |
⚠️ আপনি চাইলে এখানে Pie Chart বা Income vs Expense bar chart Google Chart বা Chart.js দিয়ে যুক্ত করতে পারেন।
খোঁজ করুন (Search Feature)
<!– Search Form –>
<form method=”get”>
<input type=”text” name=”search” placeholder=”Title দিয়ে খুঁজুন” style=”padding: 8px; width: 250px;”>
<input type=”submit” value=”Search” style=”padding: 8px 12px; background: #3498db; color: white; border: none;”>
</form>
<?php
include ‘db.php’;
$search = ”;
if (isset($_GET[‘search’])) {
$search = $conn->real_escape_string($_GET[‘search’]);
$query = “SELECT * FROM expenses WHERE title LIKE ‘%$search%’ ORDER BY created_at DESC”;
} else {
$query = “SELECT * FROM expenses ORDER BY created_at DESC”;
}
$result = $conn->query($query);
echo “<table style=’width:100%; border-collapse: collapse; border:1px solid #ccc;’>”;
echo “<tr style=’background:#3498db; color:white;’><th>Title</th><th>Amount</th><th>Type</th><th>Date</th></tr>”;
while($row = $result->fetch_assoc()) {
echo “<tr><td style=’padding:10px;’>” . $row[‘title’] . “</td><td>” . $row[‘amount’] . “</td><td>” . $row[‘type’] . “</td><td>” . $row[‘created_at’] . “</td></tr>”;
}
echo “</table>”;
?>
খরচের তালিকা + মোট হিসাব
<!– Search Form –>
<form method=”get”>
<input type=”text” name=”search” placeholder=”Title দিয়ে খুঁজুন” style=”padding: 8px; width: 250px;”>
<input type=”submit” value=”Search” style=”padding: 8px 12px; background: #3498db; color: white; border: none;”>
</form>
<?php
include ‘db.php’;
$search = ”;
if (isset($_GET[‘search’])) {
$search = $conn->real_escape_string($_GET[‘search’]);
$query = “SELECT * FROM expenses WHERE title LIKE ‘%$search%’ ORDER BY created_at DESC”;
} else {
$query = “SELECT * FROM expenses ORDER BY created_at DESC”;
}
$result = $conn->query($query);
$total = 0;
$income = 0;
$expense = 0;
echo “<table style=’width:100%; border-collapse: collapse; border:1px solid #ccc;’>”;
echo “<tr style=’background:#3498db; color:white;’><th>Title</th><th>Amount</th><th>Type</th><th>Date</th></tr>”;
while($row = $result->fetch_assoc()) {
$amount = $row[‘amount’];
$type = strtolower($row[‘type’]);
if ($type == ‘income’) $income += $amount;
else if ($type == ‘expense’) $expense += $amount;
$total += ($type == ‘income’) ? $amount : -$amount;
echo “<tr><td style=’padding:10px;’>” . $row[‘title’] . “</td><td>” . $row[‘amount’] . “</td><td>” . $row[‘type’] . “</td><td>” . $row[‘created_at’] . “</td></tr>”;
}
echo “</table><br>”;
echo “<table style=’width:100%; border:2px solid #27ae60; background:#e8f5e9; padding:10px; margin-top:20px;’>”;
echo “<tr><th style=’text-align:left;’>✅ মোট ইনকাম</th><td>” . $income . ” ৳</td></tr>”;
echo “<tr><th style=’text-align:left;’>❌ মোট খরচ</th><td>” . $expense . ” ৳</td></tr>”;
echo “<tr><th style=’text-align:left;’> ব্যালেন্স</th><td>” . $total . ” ৳</td></tr>”;
echo “</table>”;
?>
Search ফর্মের নিচে টেবিল আকারে ডেটা দেখাবে:
| Title | Amount | Type | Date |
|---|---|---|---|
| Salary | 10000 | Income | 2025-05-10 |
| Lunch | 300 | Expense | 2025-05-11 |
নিচে মোট হিসাব দেখাবে:
| ✅ মোট ইনকাম | 10000 ৳ |
|---|---|
| ❌ মোট খরচ | 300 ৳ |
| ব্যালেন্স | 9700 ৳ |
ইনকাম ও খরচ রিপোর্ট (Pie + Bar Chart)
<!– Google Charts Load –>
<script type=”text/javascript” src=”https://www.gstatic.com/charts/loader.js”></script>
<?php
include ‘db.php’;
// Pie Chart Data
$income = 0;
$expense = 0;
$res = $conn->query(“SELECT type, SUM(amount) as total FROM expenses GROUP BY type”);
while ($row = $res->fetch_assoc()) {
if (strtolower($row[‘type’]) == ‘income’) $income = $row[‘total’];
else $expense = $row[‘total’];
}
// Bar Chart Data (Month Wise)
$barData = [];
$res2 = $conn->query(“SELECT DATE_FORMAT(created_at, ‘%Y-%m’) as month, type, SUM(amount) as total FROM expenses GROUP BY month, type ORDER BY month ASC”);
while ($row = $res2->fetch_assoc()) {
$month = $row[‘month’];
$type = strtolower($row[‘type’]);
if (!isset($barData[$month])) $barData[$month] = [‘income’ => 0, ‘expense’ => 0];
$barData[$month][$type] = $row[‘total’];
}
?>
<script type=”text/javascript”>
google.charts.load(‘current’, {‘packages’:[‘corechart’]});
google.charts.setOnLoadCallback(drawCharts);
function drawCharts() {
// Pie Chart
var pieData = google.visualization.arrayToDataTable([
[‘Type’, ‘Amount’],
[‘Income’, <?= $income ?>],
[‘Expense’, <?= $expense ?>]
]);
var pieOptions = { title: ‘মোট হিসাব’, is3D: true };
var pieChart = new google.visualization.PieChart(document.getElementById(‘piechart’));
pieChart.draw(pieData, pieOptions);
// Bar Chart
var barData = google.visualization.arrayToDataTable([
[‘Month’, ‘Income’, ‘Expense’],
<?php
foreach ($barData as $month => $amounts) {
echo “[‘$month’, {$amounts[‘income’]}, {$amounts[‘expense’]}],”;
}
?>
]);
var barOptions = {
title: ‘মাসভিত্তিক ইনকাম ও খরচ’,
hAxis: {title: ‘Month’},
vAxis: {title: ‘Amount’},
colors: [‘#4caf50’, ‘#e53935’]
};
var barChart = new google.visualization.ColumnChart(document.getElementById(‘barchart’));
barChart.draw(barData, barOptions);
}
</script>
<!– ️ Chart Divs –>
<div id=”piechart” style=”width: 100%; height: 400px;”></div>
<div id=”barchart” style=”width: 100%; height: 450px;”></div>