Is there a recommended way to mark a task as completed in a PHP forum thread like this?
To mark a task as completed in a PHP forum thread, you can create a checkbox or button next to the task that, when clicked, updates a database field to indicate that the task is completed. You can then display completed tasks differently, such as with a strike-through or a different color.
// Assuming you have a tasks table in your database with a column 'completed'
// Connect to your database
$pdo = new PDO('mysql:host=localhost;dbname=your_database', 'username', 'password');
// Update task as completed
if(isset($_POST['task_id'])) {
$task_id = $_POST['task_id'];
$stmt = $pdo->prepare("UPDATE tasks SET completed = 1 WHERE id = :task_id");
$stmt->bindParam(':task_id', $task_id);
$stmt->execute();
}
// Display tasks
$stmt = $pdo->query("SELECT * FROM tasks");
while ($row = $stmt->fetch()) {
$task_id = $row['id'];
$task_name = $row['name'];
$completed = $row['completed'];
if($completed) {
echo "<del>$task_name</del> <span style='color: green;'>(Completed)</span><br>";
} else {
echo "$task_name <form method='post'><input type='hidden' name='task_id' value='$task_id'><input type='submit' value='Mark as Completed'></form><br>";
}
}