PHP To-Do List Project 🎯

beginner
18 min

PHP To-Do List Project 🎯

Welcome to the PHP To-Do List Project! In this comprehensive guide, we'll build a practical To-Do application that will help you understand and master PHP from the ground up.

What is PHP? πŸ“

PHP (Hypertext Preprocessor) is a popular server-side scripting language used to create dynamic web pages. It's free, open-source, and runs on various platforms (Windows, Linux, Unix, etc.).

Why PHP? πŸ’‘

PHP is widely used because of its simplicity, flexibility, and strong community support. It's an excellent choice for beginners due to its straightforward syntax and quick learning curve.

Setting Up Your Environment πŸ’»

Before we dive into the project, let's set up a local development environment. We recommend using XAMPP for Windows or MAMP for macOS.

Starting the To-Do List Project πŸ“

Step 1: Creating the Project Structure πŸ“

  • Create a new folder for your project, e.g., todo-list.
  • Inside the folder, create the following directories:
    • css
    • js
    • include
    • pages

Step 2: Creating Basic HTML Structure πŸ› οΈ

  • Inside the todo-list folder, create an index.php file.
  • Add the basic HTML structure for your To-Do List application.
html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>To-Do List</title> </head> <body> <!-- Your To-Do List code will go here --> </body> </html>

Step 3: Adding PHP to index.php πŸ“

  • Replace the body content with PHP code to create a simple To-Do List interface.
php
<?php echo "<h1>To-Do List</h1>"; echo "<form action='add_task.php' method='post'>"; echo "<input type='text' name='task' placeholder='Add a task'>"; echo "<input type='submit' value='Add Task'>"; echo "</form>"; ?>

Step 4: Creating add_task.php πŸ“

  • In the include folder, create a file named add_task.php.
  • This script will handle the form submission and store the tasks in an array.
php
<?php $tasks = []; if ($_SERVER['REQUEST_METHOD'] == 'POST') { $task = $_POST['task']; array_push($tasks, $task); } include 'view.php'; ?>

Step 5: Creating view.php πŸ“

  • In the include folder, create a file named view.php.
  • This script will display the tasks.
php
<?php function displayTasks($tasks) { if (!empty($tasks)) { echo "<ul>"; foreach ($tasks as $task) { echo "<li>$task</li>"; } echo "</ul>"; } else { echo "<p>No tasks yet.</p>"; } } ?>

Step 6: Updating index.php to display tasks πŸ“

  • In the index.php file, include the view.php script and call the displayTasks function.
php
<?php include 'include/view.php'; displayTasks($tasks); ?>
Quick Quiz
Question 1 of 1

What is PHP used for?

Now you have a basic To-Do List application! As you continue to build this project, we'll cover more PHP concepts, such as variables, functions, arrays, and databases.

Happy coding! πŸ’»πŸ’ΌπŸš€