Skip to content
Youths Forum Youths Forum Youths Forum

Tech Blogs & Programming Tutorials

Youths Forum Youths Forum Youths Forum

Tech Blogs & Programming Tutorials

  • Blog
  • News
  • Programming
    • PHP
    • JavaScript
    • JQuery
    • CSS
    • HTML
    • API
  • Stock Market Live
  • Automobiles
    • Cars
  • Gadgets
    • Phones
    • Android Phones

Categories

  • Automobiles (12)
    • Cars (7)
  • Blog (103)
    • Poems (2)
    • Space (2)
  • Command (2)
  • Education (2)
  • Entertainment (4)
  • Gadgets (9)
    • Phones (8)
      • Android Phones (4)
  • HTML Templates (11)
  • IT Training Institutes (1)
  • Lifestyle (4)
  • News (51)
  • Others (23)
  • Programming (296)
    • API (16)
    • CSS (83)
    • Database (4)
    • Hosting (1)
    • HTML (37)
    • JavaScript (117)
      • JQuery (27)
      • ReactJS (7)
    • PHP (116)
  • Python (3)
  • recipes (1)
  • SEE Result (1)
  • Server (3)
  • Blog
  • News
  • Programming
    • PHP
    • JavaScript
    • JQuery
    • CSS
    • HTML
    • API
  • Stock Market Live
  • Automobiles
    • Cars
  • Gadgets
    • Phones
    • Android Phones
Close

Search

PHPProgramming

Building a Simple To-Do List with PHP and SQLite

By Admin
February 9, 2025 4 Min Read
0

Managing tasks efficiently is crucial for productivity, and having a simple to-do list can help keep things organized. In this article, we will discuss the concept of creating a lightweight to-do list application using PHP and SQLite, a self-contained, serverless database engine.

Why Use SQLite?

SQLite is a powerful yet lightweight database management system. Unlike MySQL or PostgreSQL, SQLite does not require a separate server to operate. Instead, it stores all data in a single file, making it an excellent choice for small-scale applications, testing, and embedded systems.

Advantages of SQLite:

  • Simplicity: No need to install and configure a separate database server.
  • Lightweight: Stores data in a single file, making it highly portable.
  • Self-contained: Suitable for applications that do not require a client-server architecture.
  • Fast and Reliable: Performs efficiently for smaller applications.

Follow this video for complete guidance :

To Do List with PHP and SQLite – Source Code

<?php
$db = new SQLite3('todo.db');

$query = "CREATE TABLE IF NOT EXISTS tasks (
                    id INTEGER PRIMARY KEY AUTOINCREMENT,
                    task TEXT NOT NULL,
                    completed INTEGER DEFAULT 0
                  )";
$db->query($query);

// Handle form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (isset($_POST['add_task'])) {
        $task = $_POST['task'];
        $stmt = $db->prepare('INSERT INTO tasks (task) VALUES (:task)');
        $stmt->bindValue(':task', $task, SQLITE3_TEXT);
        $stmt->execute();
    } elseif (isset($_POST['delete_task'])) {
        $id = $_POST['id'];
        $stmt = $db->prepare('DELETE FROM tasks WHERE id = :id');
        $stmt->bindValue(':id', $id, SQLITE3_INTEGER);
        $stmt->execute();
    } elseif (isset($_POST['toggle_task'])) {
        $id = $_POST['id'];
        $stmt = $db->prepare('UPDATE tasks SET completed = 1 - completed WHERE id = :id');
        $stmt->bindValue(':id', $id, SQLITE3_INTEGER);
        $stmt->execute();
    } elseif (isset($_POST['clear_all'])) {
        $db->query('DELETE FROM tasks');
    }
    header('Location: index.php');
}

// Fetch all tasks
$result = $db->query('SELECT * FROM tasks ORDER BY id DESC');
$tasks = [];
while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
    $tasks[] = $row;
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>To-Do List</title>
    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    <style>
        body {
            background: linear-gradient(135deg, #667eea, #764ba2);
            font-family: 'Poppins', sans-serif;
            min-height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
            color: #333;
        }
        .container {
            background: rgba(255, 255, 255, 0.9);
            border-radius: 15px;
            padding: 30px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
            max-width: 600px;
            width: 100%;
        }
        h1 {
            font-size: 2.5rem;
            font-weight: 700;
            color: #333;
            text-align: center;
            margin-bottom: 20px;
        }
        .form-control {
            border-radius: 10px;
            border: 2px solid #ddd;
            padding: 10px 15px;
            font-size: 1rem;
        }
        .form-control:focus {
            border-color: #667eea;
            box-shadow: none;
        }
        .btn-primary {
            background: #667eea;
            border: none;
            border-radius: 10px;
            padding: 10px 20px;
            font-size: 1rem;
            transition: background 0.3s ease;
        }
        .btn-primary:hover {
            background: #764ba2;
        }
        .task-list {
            margin-top: 20px;
            height: 400px;
            overflow: auto  ;
        }
        .task-item {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 15px;
            border: 2px solid #ddd;
            border-radius: 10px;
            margin-bottom: 10px;
            background: #fff;
            transition: all 0.3s ease;
        }
        .task-item:hover {
            transform: translateY(-3px);
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
        }
        .task-text {
            margin: 0;
            flex-grow: 1;
            margin-left: 10px;
            font-size: 1.1rem;
        }
        .task-text.completed {
            text-decoration: line-through;
            color: #888;
        }
        .task-actions {
            display: flex;
            gap: 10px;
        }
        .btn-action {
            background: none;
            border: none;
            cursor: pointer;
            padding: 5px;
            transition: color 0.3s ease;
        }
        .btn-action:hover {
            color: #667eea;
        }
        .btn-clear {
            width: 100%;
            margin-top: 20px;
            background: #ff4d4d;
            border: none;
            border-radius: 10px;
            padding: 10px 20px;
            font-size: 1rem;
            color: #fff;
            transition: background 0.3s ease;
        }
        .btn-clear:hover {
            background: #ff1a1a;
        }
        .badge {
            font-size: 12px;
            padding: 5px 10px;
        }
        .empty-state {
            text-align: center;
            color: #888;
            font-size: 1.2rem;
            margin-top: 20px;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>To-Do List</h1>
        <form method="POST" action="" class="d-flex mb-4">
            <input type="text" name="task" class="form-control me-2" placeholder="Add a new task" required>
            <button type="submit" name="add_task" class="btn btn-primary">Add</button>
        </form>
        <div class="task-list">
            <?php if (empty($tasks)){ ?>
                <div class="empty-state">No tasks found. Add a new task!</div>
            <?php }else{ ?>
                <?php foreach ($tasks as $task){ ?>
                    <div class="task-item">
                        <form method="POST" action="" class="d-flex align-items-center" style="flex-grow: 1;">
                            <input type="hidden" name="id" value="<?php echo $task['id']; ?>">
                            <button type="submit" name="toggle_task" class="btn-action">
                                <i class="fas <?php echo $task['completed'] ? 'fa-check-circle text-success' : 'fa-circle'; ?>"></i>
                            </button>
                            <p class="task-text <?php echo $task['completed'] ? 'completed' : ''; ?>">
                                <?php echo htmlspecialchars($task['task']); ?>
                            </p>
                        </form>
                        <div class="task-actions">
                            <form method="POST" action="">
                                <input type="hidden" name="id" value="<?php echo $task['id']; ?>">
                                <button type="submit" name="delete_task" class="btn-action text-danger">
                                    <i class="fas fa-trash"></i>
                                </button>
                            </form>
                        </div>
                    </div>
                <?php } ?>
                <form method="POST" action="">
                    <button type="submit" name="clear_all" class="btn btn-clear">Clear All Tasks</button>
                </form>
            <?php } ?>
        </div>
    </div>
</body>
</html>

 

Developing the To-Do List Application

Our to-do list application allows users to:

  • Add new tasks.
  • Mark tasks as completed.
  • Delete tasks.
  • Clear all tasks.

The application is powered by PHP and SQLite, ensuring a smooth and efficient user experience.

Key Features of the Application

  1. Database Management with SQLite
    • The application initializes an SQLite database (todo.db).
    • A tasks table is created with columns for task ID, task description, and completion status.
  2. Handling User Actions
    • Users can add tasks using a form.
    • Clicking on a task toggles its completion status.
    • Tasks can be deleted individually or cleared entirely.
  3. Interactive UI with Bootstrap
    • The front-end is styled with Bootstrap for a clean and modern look.
    • Icons and visual elements enhance usability.

How SQLite Enhances This Application

  • Data Persistence: Unlike session-based storage, SQLite keeps tasks saved even after refreshing or closing the browser.
  • Minimal Configuration: No need for complex database setup—just an SQLite file.
  • Scalability: While SQLite is not ideal for high-concurrency applications, it works perfectly for lightweight applications like this to-do list.

Using PHP and SQLite together makes building a functional and efficient to-do list application simple. This setup is ideal for small projects, testing, and learning database concepts without requiring extensive server configurations.

If you’re looking for a lightweight, serverless solution for managing structured data, SQLite is an excellent choice. It provides reliability and ease of use, making it perfect for small-scale web applications. Whether you’re a beginner or an experienced developer, experimenting with SQLite can help enhance your understanding of database-driven web applications.

Tags:

PHPtodo
Author

Admin

Follow Me
Other Articles
Previous

Currency Converter Web App Using CurrencyLayer API

Next

Nepal Engineers’ Association Holds Discussion on National AI Policy Draft

No Comment! Be the first one.

Leave a Reply

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

FIFA World Cup 2026 Predict and Win by SportsGuff

Recent Posts

  • Unpacking Nepal’s Record Rs 2.12 Trillion Budget and What It Means for You
  • How to Write a Strong Scholarship Application: The Ultimate Step-by-Step Guide
  • How to Prepare for Exams Without Stress: The Ultimate Science-Backed Guide
  • Chiranjibi Adhikari Appointed Acting President of CAN Federation
  • How to Design a Student Marksheet Using HTML and CSS

Tags

adsense ai animate animation animation using HTML and CSS API blog calculator chatgpt Cryptocurrency CSS css animation design Email Facebook featured filemanager file manager free template google htaccess HTML image Instagram interview javascript JQuery jquery ui NADA AutoShow NADA Auto Show 2024 password PHP Progressive Web App PWA QR random react reactjs Rotate travel Twitter vpn youthforum youthsforum youtube

About Us

At Youths Forum, we are passionate about sharing knowledge that empowers students, educators, professionals, and technology enthusiasts.

Our Mission

Our mission is simple: to make technology and education accessible, understandable, and beneficial for everyone. We strive to create content that helps our readers learn new skills and stay updated with industry developments.

RSS RSS

  • Unpacking Nepal’s Record Rs 2.12 Trillion Budget and What It Means for You Admin
  • How to Write a Strong Scholarship Application: The Ultimate Step-by-Step Guide Admin
  • How to Prepare for Exams Without Stress: The Ultimate Science-Backed Guide Admin

Quick Links

  • Stock Market Live
  • Parliament Election 2082
Copyright 2026 — Youths Forum. All rights reserved. Blogsy WordPress Theme