PHP Tutorial: Building an Employee Directory 🎯

beginner
16 min

PHP Tutorial: Building an Employee Directory 🎯

Welcome to this comprehensive PHP tutorial where we'll build an Employee Directory! By the end of this lesson, you'll have a solid understanding of PHP, perfect for beginners and intermediates. Let's dive in!

Introduction πŸ“

PHP (Hypertext Preprocessor) is a popular, server-side scripting language used for web development. In this project, we'll create a simple Employee Directory that stores employee details in a database and displays them on a webpage.

Prerequisites

Before we start, ensure you have:

  1. A working PHP environment (e.g., XAMPP, WAMP, MAMP, or local servers provided by your hosting service)
  2. A text editor (e.g., Notepad++, Sublime Text, Visual Studio Code)

Setting Up the Project Structure πŸ“

Create a new folder named employee_directory and inside it, create the following files and folders:

  • index.php
  • config.php
  • database.php
  • functions.php
  • style.css

Configuring the Database πŸ“

In this step, we'll create a MySQL database for our Employee Directory.

  1. Open your PHP MySQL client (e.g., phpMyAdmin) and create a new database named employee_directory.

  2. Create a table named employees with the following structure:

    • id (INT, AUTO_INCREMENT, PRIMARY KEY)
    • first_name (VARCHAR)
    • last_name (VARCHAR)
    • email (VARCHAR)
    • position (VARCHAR)
    • department (VARCHAR)

Connecting to the Database πŸ“

Next, let's connect our PHP files to the database.

  1. In config.php, define your database credentials:
php
<?php $db_host = "localhost"; $db_user = "your_database_username"; $db_pass = "your_database_password"; $db_name = "employee_directory";
  1. In database.php, create the database connection:
php
<?php class Database { // ... (database connection code here) }

Creating Functions πŸ“

Now, let's create useful functions for our Employee Directory.

  1. In functions.php, add the following functions:
  • getEmployees(): Retrieves all employee data from the database.
  • addEmployee(): Inserts a new employee into the database.
  • deleteEmployee(): Deletes an employee from the database.
  • editEmployee(): Updates an employee's information in the database.

Designing the Web Interface πŸ’‘

In this section, we'll design the web interface using HTML, CSS, and PHP.

  1. In index.php, display the employee list and add, edit, and delete functionality.

  2. In style.css, style the webpage to make it visually appealing.

Testing and Debugging πŸ“

Finally, test your Employee Directory to ensure everything works correctly.

  • Add, edit, and delete employees from the database.
  • Verify the web interface displays the employee data correctly.

Quiz πŸ“

Quick Quiz
Question 1 of 1

What is the name of the database created for the Employee Directory?

That's it! You now have a functional Employee Directory built with PHP. This project should give you a good understanding of PHP's practical applications and provide a solid foundation for further learning. Happy coding! πŸš€