PHP Todo-list

<?php
// タスクを保存するファイル
$tasks_file = 'tasks.txt';

// タスクを追加
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['new_task'])) {
    $new_task = trim($_POST['new_task']);
    if (!empty($new_task)) {
        $task_entry = $new_task . '|0'; // 0は未完了ステータスを示す
        file_put_contents($tasks_file, $task_entry . PHP_EOL, FILE_APPEND);
    }
}

// タスクの完了ステータスを変更
if (isset($_GET['toggle'])) {
    $tasks = file($tasks_file, FILE_IGNORE_NEW_LINES);
    $index = $_GET['toggle'];
    if (isset($tasks[$index])) {
        $task_data = explode('|', $tasks[$index]);
        $task_data[1] = $task_data[1] == '0' ? '1' : '0'; // ステータスを切り替える
        $tasks[$index] = implode('|', $task_data);
        file_put_contents($tasks_file, implode(PHP_EOL, $tasks) . PHP_EOL);
    }
}

// タスクを削除
if (isset($_GET['remove'])) {
    $tasks = file($tasks_file, FILE_IGNORE_NEW_LINES);
    unset($tasks[$_GET['remove']]);
    file_put_contents($tasks_file, implode(PHP_EOL, $tasks) . PHP_EOL);
}

// 現在のタスクを取得
$tasks = file_exists($tasks_file) ? file($tasks_file, FILE_IGNORE_NEW_LINES) : [];
?>

<!DOCTYPE html>
<html lang="ja">
<head>
    <meta charset="UTF-8">
    <title>TODOリスト</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f4;
            margin: 0;
            padding: 20px;
        }
        h1 {
            text-align: center;
        }
        form {
            display: flex;
            justify-content: center;
            margin-bottom: 20px;
        }
        input[type="text"] {
            width: 300px;
            padding: 10px;
            border: 1px solid #ccc;
            border-radius: 5px;
        }
        button {
            padding: 10px 20px;
            border: none;
            background-color: #5cb85c;
            color: white;
            border-radius: 5px;
            cursor: pointer;
        }
        button:hover {
            background-color: #4cae4c;
        }
        ul {
            list-style-type: none;
            padding: 0;
        }
        li {
            background-color: #fff;
            margin: 5px 0;
            padding: 10px;
            border: 1px solid #ccc;
            border-radius: 5px;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        .completed {
            text-decoration: line-through;
            color: gray;
        }
        a {
            text-decoration: none;
            color: #d9534f;
            margin-left: 10px;
        }
        a:hover {
            color: #c9302c;
        }
    </style>
</head>
<body>
    <h1>TODOリスト</h1>

    <!-- タスクを追加するフォーム -->
    <form method="post" action="">
        <input type="text" name="new_task" placeholder="新しいタスクを入力" required>
        <button type="submit">追加</button>
    </form>

    <!-- タスクを表示 -->
    <ul>
        <?php foreach ($tasks as $index => $task): ?>
            <?php list($task_text, $status) = explode('|', $task); ?>
            <li>
                <span class="<?php echo $status == '1' ? 'completed' : ''; ?>">
                    <?php echo htmlspecialchars($task_text); ?>
                </span>
                <div>
                    <a href="?toggle=<?php echo $index; ?>">
                        <?php echo $status == '0' ? '完了にする' : '未完了に戻す'; ?>
                    </a>
                    <a href="?remove=<?php echo $index; ?>">削除</a>
                </div>
            </li>
        <?php endforeach; ?>
    </ul>
</body>
</html>

投稿者: chosuke

趣味はゲームやアニメや漫画などです

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です