要創建一個todo列表應用,你可以使用PHP編寫一個簡單的Web應用,并結合HTML和CSS來呈現界面。以下是一個簡單的示例:
CREATE TABLE todos (
id INT AUTO_INCREMENT PRIMARY KEY,
task VARCHAR(255) NOT NULL,
completed TINYINT(1) DEFAULT 0
);
<?php
// 連接數據庫
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "todo_list";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// 處理新增todo任務
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['task'])) {
$task = $_POST['task'];
$sql = "INSERT INTO todos (task) VALUES ('$task')";
$conn->query($sql);
}
// 獲取todo列表
$sql = "SELECT * FROM todos";
$result = $conn->query($sql);
// 顯示todo列表
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<input type='checkbox' id='task_" . $row['id'] . "' ";
if ($row['completed'] == 1) {
echo "checked";
}
echo ">";
echo "<label for='task_" . $row['id'] . "'>" . $row['task'] . "</label><br>";
}
} else {
echo "No tasks found.";
}
$conn->close();
?>
<!DOCTYPE html>
<html>
<head>
<title>Todo List</title>
</head>
<body>
<h1>Todo List</h1>
<form action="" method="post">
<input type="text" name="task" placeholder="Enter a new task">
<button type="submit">Add Task</button>
</form>
<div id="tasks">
<?php include 'todolist.php'; ?>
</div>
</body>
</html>
通過以上步驟,你可以創建一個簡單的todo列表應用,用戶可以添加新的任務并將任務標記為已完成。你可以根據需要擴展這個應用,添加更多功能和樣式。