File: /home/bigisxfd/public_html/cowork/expenses.php
<?php $itm_ref='';
// Include necessary files
include_once("header.php");
include_once("sidebar.php");
// Handle delete action
if (isset($_GET['delete_id'])) {
$delete_id = $_GET['delete_id'];
// Fetch the file name to delete it from the server
$sql = "SELECT file FROM expenses WHERE id=$delete_id";
$result = $conn->query($sql);
$row = $result->fetch_assoc();
if ($row['file']) {
$file_path = 'uploads/' . $row['file'];
if (file_exists($file_path)) {
unlink($file_path); // Delete the file
}
}
// Delete the record from the database
$sql = "DELETE FROM expenses WHERE id=$delete_id";
if ($conn->query($sql) === TRUE) {
echo "Record deleted successfully";
} else {
echo "Error deleting record: " . $conn->error;
}
}
// Filter by month
$filter_sql = '';
if (isset($_GET['month'])) {
$month = $_GET['month'];
$filter_sql = " WHERE MONTH(date) = '$month'";
}
// Fetch all expenses with sorting and filtering
$sql = "SELECT expenses.id, expenses.expense_name, expenses.description, expenses.date, expenses.amount, Users.username, expenses.file
FROM expenses
LEFT JOIN Users ON expenses.created_by = Users.userid
$filter_sql
ORDER BY expenses.date DESC";
$result = $conn->query($sql);
?>
<style>
table {
width: 100%;
border-collapse: collapse;
}
th, td {
padding: 8px;
text-align: left;
border-bottom: 1px solid #ddd;
}
th {
background-color: #f2f2f2;
}
.action-btn {
text-decoration: none;
padding: 5px 10px;
margin-right: 5px;
background-color: #4CAF50;
color: white;
border-radius: 5px;
border: none;
cursor: pointer;
}
.delete-btn {
background-color: #f44336;
}
</style>
<section>
<div class="column">
<div style="display: flex; align-items: center; justify-content: space-between;">
<h2>Expenses</h2>
<button class="add-button" onclick="location.href='add_expense.php'">Record Expense</button>
</div>
<!-- Filter by month -->
<form method="get" action="expenses.php" style="margin-bottom: 20px;">
<label for="month">Filter by Month:</label>
<select id="month" name="month" style="padding: 9px;">
<option value="">All</option>
<?php
for ($m = 1; $m <= 12; $m++) {
$month_name = date('F', mktime(0, 0, 0, $m, 10));
echo "<option value='$m'>" . $month_name . "</option>";
}
?>
</select>
<input type="submit" value="Filter">
</form>
<table>
<thead>
<tr>
<th>ID</th>
<th>Expense Name</th>
<th>Description</th>
<th>Date</th>
<th>Amount</th>
<th>Created By</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
if ($result->num_rows > 0) {
// Output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td>" . $row["id"] . "</td>";
echo "<td>" . $row["expense_name"] . "</td>";
echo "<td>" . $row["description"] . "</td>";
echo "<td>" . $row["date"] . "</td>";
echo "<td>" . number_format($row["amount"], 2) . "</td>";
echo "<td>" . $row["username"] . "</td>";
echo "<td>";
if ($row["file"]) {
echo "<a href='uploads/" . $row["file"] . "' class='action-btn' target='_blank'>View File</a>";
} else {
echo "<button class='action-btn' disabled>No File</button>";
}
echo "<a href='expenses.php?delete_id=" . $row["id"] . "' class='action-btn delete-btn' onclick='return confirm(\"Are you sure you want to delete this record?\")'>Delete</a>";
echo "</td>";
echo "</tr>";
}
} else {
echo "<tr><td colspan='7'>No expenses found</td></tr>";
}
$conn->close();
?>
</tbody>
</table>
</div>
</section>
</body>
</html>