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:
- 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.
- 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. - Binding Parameters: We bind the parameters to the prepared statement. Here,
employee_id
is an integer, whilehours_worked
andpayment_per_hour
are both floating-point numbers. - Sample Data: For demonstration purposes, we set sample values for
employee_id
,hours_worked
, andpayment_per_hour
. In a real application, these values would typically come from user input. - Executing the Statement: We execute the prepared statement to insert the data into the database.
- Calculating Total Payment: We calculate the total payment by multiplying the hours worked by the payment per hour.
- 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.
- 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! 😊
I’ve been designing web applications—on and off—since 2001, back when animated GIFs were all the rage and ‘responsive design’ meant answering your client’s emails. Over the past 14 years, I’ve kept pace with the ever-evolving trends in PHP development, successfully delivering a variety of projects that made my clients happy (and kept me caffeinated).
This website serves as my soapbox—a place to share the insights I’ve picked up along the way with anyone curious enough to dive in. Welcome aboard!
Need some custom work done? Or, just want to reach out? Email: dan@danoriordan.com