Simple Employee Work Hours Tracker

In this project, we will create a simple PHP application that tracks employee work hours. The application will calculate the total hours worked, payment per hour, and total payment at the end of the week. We will use MySQLi prepared statements for secure database interactions and Bootstrap for a responsive table layout.

<?php
// Database connection
$host = 'localhost';
$user = 'root';
$password = '';
$dbname = 'employee_tracker';

$conn = new mysqli($host, $user, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Prepare and bind
$stmt = $conn->prepare("INSERT INTO work_hours (employee_id, hours_worked, payment_per_hour) VALUES (?, ?, ?)");
$stmt->bind_param("iid", $employee_id, $hours_worked, $payment_per_hour);

// Sample data
$employee_id = 1; // Example employee ID
$hours_worked = 40; // Example hours worked
$payment_per_hour = 15.00; // Example payment per hour

// Execute the statement
$stmt->execute();

// Calculate total payment
$total_payment = $hours_worked * $payment_per_hour;

// Display results in a Bootstrap table
echo '<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">';
echo '<div class="container mt-5">';
echo '<h2>Employee Work Hours Summary</h2>';
echo '<table class="table table-bordered">';
echo '<thead><tr><th>Employee ID</th><th>Hours Worked</th><th>Payment per Hour</th><th>Total Payment</th></tr></thead>';
echo '<tbody>';
echo '<tr>';
echo '<td>' . $employee_id . '</td>';
echo '<td>' . $hours_worked . '</td>';
echo '<td>$' . number_format($payment_per_hour, 2) . '</td>';
echo '<td>$' . number_format($total_payment, 2) . '</td>';
echo '</tr>';
echo '</tbody>';
echo '</table>';
echo '</div>';

// Close the statement and connection
$stmt->close();
$conn->close();
?>

Let’s break down the code step by step:

  1. Database Connection: We start by establishing a connection to the MySQL database using MySQLi. Make sure to replace the connection parameters with your actual database credentials.
  2. Prepared Statement: We prepare an SQL statement to insert the employee’s work hours into the work_hours table. Using prepared statements helps prevent SQL injection, making our application more secure.
  3. Binding Parameters: We bind the parameters to the prepared statement. Here, employee_id is an integer, while hours_worked and payment_per_hour are both floating-point numbers.
  4. Sample Data: For demonstration purposes, we set sample values for employee_idhours_worked, and payment_per_hour. In a real application, these values would typically come from user input.
  5. Executing the Statement: We execute the prepared statement to insert the data into the database.
  6. Calculating Total Payment: We calculate the total payment by multiplying the hours worked by the payment per hour.
  7. Displaying Results: We use Bootstrap to create a responsive table that displays the employee’s work hours summary. The table includes columns for Employee ID, Hours Worked, Payment per Hour, and Total Payment.
  8. Closing Connections: Finally, we close the prepared statement to free up resources. Some will suggest to close the connection too. However, this is not necessary, and I have not included it here. PHP does this automatically.

Want to try this without connecting to a database? Try this:

<?php


// Sample data
$employee_id = 1; // Example employee ID
$hours_worked = 45; // Example hours worked
$payment_per_hour = 32.50; // Example payment per hour


// Calculate total payment
$total_payment = $hours_worked * $payment_per_hour;

// Display results in a Bootstrap table
echo '<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">';
echo '<div class="container mt-5">';
echo '<h2>Employee Work Hours Summary</h2>';
echo '<table class="table table-bordered">';
echo '<thead><tr><th>Employee ID</th><th>Hours Worked</th><th>Payment per Hour</th><th>Total Payment</th></tr></thead>';
echo '<tbody>';
echo '<tr>';
echo '<td>' . $employee_id . '</td>';
echo '<td>' . $hours_worked . '</td>';
echo '<td>$' . number_format($payment_per_hour, 2) . '</td>';
echo '<td>$' . number_format($total_payment, 2) . '</td>';
echo '</tr>';
echo '</tbody>';
echo '</table>';
echo '</div>';

?>

And there you have it! A simple yet effective employee work hours tracker using PHP, MySQLi, and Bootstrap. Feel free to customize it further to suit your needs! If you have any questions or need further assistance, just let me know! 😊

Leave a Reply

Your email address will not be published. Required fields are marked *