要创建一个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列表应用,用户可以添加新的任务并将任务标记为已完成。你可以根据需要扩展这个应用,添加更多功能和样式。