Expense Tracker for Students with PHP

Managing finances is a crucial skill for students, and developing an Expense Tracker can be an insightful and practical project. In this article, we’ll guide you through the process of creating an Expense Tracker for Students using PHP, a server-side scripting language that is commonly used for web development.

Understanding the Project

An Expense Tracker for Students is a web application that allows users to log their expenses, categorize them, and track their spending habits over time. It provides insights into where money is going and helps students make informed financial decisions.

Setting Up the Project

Before diving into the code, make sure you have PHP installed on your server. You can use tools like XAMPP, WampServer, or MAMP for local development.

Create Project Directory:

mkdir expense-tracker
cd expense-tracker

Initialize PHP Project:

php init

Project Structure:

expense-tracker/
├── index.php
├── add_expense.php
├── view_expenses.php
├── db_config.php
├── assets/
   └── style.css
└── includes/
    └── functions.php

Creating the Database

We’ll use MySQL for the database. Create a database named expense_tracker and a table named expenses with fields like id, title, amount, category, and timestamp.

CREATE TABLE expenses (
    id INT AUTO_INCREMENT PRIMARY KEY,
    title VARCHAR(255) NOT NULL,
    amount DECIMAL(10, 2) NOT NULL,
    category VARCHAR(50) NOT NULL,
    timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

Coding the Expense Tracker

1. db_config.php

<?php
$host = "localhost";
$username = "root";
$password = "";
$database = "expense_tracker";

$conn = mysqli_connect($host, $username, $password, $database);

if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
?>

2. includes/functions.php

<?php
include_once 'db_config.php';

function addExpense($title, $amount, $category) {
    global $conn;
    $query = "INSERT INTO expenses (title, amount, category) VALUES ('$title', $amount, '$category')";
    return mysqli_query($conn, $query);
}

function getExpenses() {
    global $conn;
    $query = "SELECT * FROM expenses ORDER BY timestamp DESC";
    return mysqli_query($conn, $query);
}
?>

3. add_expense.php

<?php
include_once 'includes/functions.php';

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $title = $_POST["title"];
    $amount = $_POST["amount"];
    $category = $_POST["category"];

    addExpense($title, $amount, $category);
}
?>

4. view_expenses.php

<?php
include_once 'includes/functions.php';

$expenses = getExpenses();
?>

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Expense Tracker - View Expenses</title>
    <link rel="stylesheet" href="assets/style.css">
</head>

<body>
    <h1>Expense Tracker</h1>

    <h2>View Expenses</h2>

    <table>
        <thead>
            <tr>
                <th>Title</th>
                <th>Amount</th>
                <th>Category</th>
                <th>Date</th>
            </tr>
        </thead>
        <tbody>
            <?php while ($row = mysqli_fetch_assoc($expenses)) : ?>
                <tr>
                    <td><?= $row['title']; ?></td>
                    <td><?= $row['amount']; ?></td>
                    <td><?= $row['category']; ?></td>
                    <td><?= $row['timestamp']; ?></td>
                </tr>
            <?php endwhile; ?>
        </tbody>
    </table>

    <a href="index.php">Back to Home</a>
</body>

</html>

5. index.php

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Expense Tracker</title>
    <link rel="stylesheet" href="assets/style.css">
</head>

<body>
    <h1>Expense Tracker</h1>

    <h2>Add Expense</h2>
    <form action="add_expense.php" method="post">
        <label for="title">Title:</label>
        <input type="text" name="title" required>

        <label for="amount">Amount:</label>
        <input type="number" name="amount" required>

        <label for="category">Category:</label>
        <input type="text" name="category" required>

        <button type="submit">Add Expense</button>
    </form>

    <h2>View Expenses</h2>
    <a href="view_expenses.php">View All Expenses</a>
</body>

</html>

6. assets/style.css

body {
    font-family: Arial, sans-serif;
    margin: 20px;
}

h1, h2 {
    color: #333;
}

form {
    display: grid;
    gap: 10px;
    max-width: 400px;
}

table {
    width: 100%;
    border-collapse: collapse;
    margin-top: 20px;
}

table, th, td {
    border: 1px solid #ddd;
}

th, td {
    padding: 8px;
    text-align: left;
}

a {
    display: inline-block;
    margin-top: 10px;
    padding: 5px 10px;
    background-color: #333;
    color: #fff;
    text-decoration: none;
}

Running the Project

  1. Place the project folder in your server’s root directory.
  2. Start your server (e.g., using XAMPP or MAMP).
  3. Access the project in your browser (e.g., http://localhost/expense-tracker).

Congratulations! You’ve created a simple yet functional Expense Tracker for Students using PHP. This project lays the foundation for further enhancements such as user authentication, detailed reporting, and budget analysis. Feel free to customize and expand the project to meet your specific requirements. Happy coding!