{"id":25527,"date":"2024-09-26T22:17:08","date_gmt":"2024-09-26T13:17:08","guid":{"rendered":"http:\/\/www.tyosuke20xx.com\/blog\/?p=25527"},"modified":"2024-09-26T22:17:10","modified_gmt":"2024-09-26T13:17:10","slug":"php-todo-list","status":"publish","type":"post","link":"http:\/\/www.tyosuke20xx.com\/blog\/?p=25527","title":{"rendered":"PHP Todo-list"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>&lt;?php\n\/\/ \u30bf\u30b9\u30af\u3092\u4fdd\u5b58\u3059\u308b\u30d5\u30a1\u30a4\u30eb\n$tasks_file = 'tasks.txt';\n\n\/\/ \u30bf\u30b9\u30af\u3092\u8ffd\u52a0\nif ($_SERVER&#91;'REQUEST_METHOD'] === 'POST' &amp;&amp; isset($_POST&#91;'new_task'])) {\n    $new_task = trim($_POST&#91;'new_task']);\n    if (!empty($new_task)) {\n        $task_entry = $new_task . '|0'; \/\/ 0\u306f\u672a\u5b8c\u4e86\u30b9\u30c6\u30fc\u30bf\u30b9\u3092\u793a\u3059\n        file_put_contents($tasks_file, $task_entry . PHP_EOL, FILE_APPEND);\n    }\n}\n\n\/\/ \u30bf\u30b9\u30af\u306e\u5b8c\u4e86\u30b9\u30c6\u30fc\u30bf\u30b9\u3092\u5909\u66f4\nif (isset($_GET&#91;'toggle'])) {\n    $tasks = file($tasks_file, FILE_IGNORE_NEW_LINES);\n    $index = $_GET&#91;'toggle'];\n    if (isset($tasks&#91;$index])) {\n        $task_data = explode('|', $tasks&#91;$index]);\n        $task_data&#91;1] = $task_data&#91;1] == '0' ? '1' : '0'; \/\/ \u30b9\u30c6\u30fc\u30bf\u30b9\u3092\u5207\u308a\u66ff\u3048\u308b\n        $tasks&#91;$index] = implode('|', $task_data);\n        file_put_contents($tasks_file, implode(PHP_EOL, $tasks) . PHP_EOL);\n    }\n}\n\n\/\/ \u30bf\u30b9\u30af\u3092\u524a\u9664\nif (isset($_GET&#91;'remove'])) {\n    $tasks = file($tasks_file, FILE_IGNORE_NEW_LINES);\n    unset($tasks&#91;$_GET&#91;'remove']]);\n    file_put_contents($tasks_file, implode(PHP_EOL, $tasks) . PHP_EOL);\n}\n\n\/\/ \u73fe\u5728\u306e\u30bf\u30b9\u30af\u3092\u53d6\u5f97\n$tasks = file_exists($tasks_file) ? file($tasks_file, FILE_IGNORE_NEW_LINES) : &#91;];\n?>\n\n&lt;!DOCTYPE html>\n&lt;html lang=\"ja\">\n&lt;head>\n    &lt;meta charset=\"UTF-8\">\n    &lt;title>TODO\u30ea\u30b9\u30c8&lt;\/title>\n    &lt;style>\n        body {\n            font-family: Arial, sans-serif;\n            background-color: #f4f4f4;\n            margin: 0;\n            padding: 20px;\n        }\n        h1 {\n            text-align: center;\n        }\n        form {\n            display: flex;\n            justify-content: center;\n            margin-bottom: 20px;\n        }\n        input&#91;type=\"text\"] {\n            width: 300px;\n            padding: 10px;\n            border: 1px solid #ccc;\n            border-radius: 5px;\n        }\n        button {\n            padding: 10px 20px;\n            border: none;\n            background-color: #5cb85c;\n            color: white;\n            border-radius: 5px;\n            cursor: pointer;\n        }\n        button:hover {\n            background-color: #4cae4c;\n        }\n        ul {\n            list-style-type: none;\n            padding: 0;\n        }\n        li {\n            background-color: #fff;\n            margin: 5px 0;\n            padding: 10px;\n            border: 1px solid #ccc;\n            border-radius: 5px;\n            display: flex;\n            justify-content: space-between;\n            align-items: center;\n        }\n        .completed {\n            text-decoration: line-through;\n            color: gray;\n        }\n        a {\n            text-decoration: none;\n            color: #d9534f;\n            margin-left: 10px;\n        }\n        a:hover {\n            color: #c9302c;\n        }\n    &lt;\/style>\n&lt;\/head>\n&lt;body>\n    &lt;h1>TODO\u30ea\u30b9\u30c8&lt;\/h1>\n\n    &lt;!-- \u30bf\u30b9\u30af\u3092\u8ffd\u52a0\u3059\u308b\u30d5\u30a9\u30fc\u30e0 -->\n    &lt;form method=\"post\" action=\"\">\n        &lt;input type=\"text\" name=\"new_task\" placeholder=\"\u65b0\u3057\u3044\u30bf\u30b9\u30af\u3092\u5165\u529b\" required>\n        &lt;button type=\"submit\">\u8ffd\u52a0&lt;\/button>\n    &lt;\/form>\n\n    &lt;!-- \u30bf\u30b9\u30af\u3092\u8868\u793a -->\n    &lt;ul>\n        &lt;?php foreach ($tasks as $index => $task): ?>\n            &lt;?php list($task_text, $status) = explode('|', $task); ?>\n            &lt;li>\n                &lt;span class=\"&lt;?php echo $status == '1' ? 'completed' : ''; ?>\">\n                    &lt;?php echo htmlspecialchars($task_text); ?>\n                &lt;\/span>\n                &lt;div>\n                    &lt;a href=\"?toggle=&lt;?php echo $index; ?>\">\n                        &lt;?php echo $status == '0' ? '\u5b8c\u4e86\u306b\u3059\u308b' : '\u672a\u5b8c\u4e86\u306b\u623b\u3059'; ?>\n                    &lt;\/a>\n                    &lt;a href=\"?remove=&lt;?php echo $index; ?>\">\u524a\u9664&lt;\/a>\n                &lt;\/div>\n            &lt;\/li>\n        &lt;?php endforeach; ?>\n    &lt;\/ul>\n&lt;\/body>\n&lt;\/html>\n\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"footnotes":""},"categories":[50,4],"tags":[38,3],"class_list":["post-25527","post","type-post","status-publish","format-standard","hentry","category-php","category-programming","tag-php","tag-programming"],"aioseo_notices":[],"jetpack_featured_media_url":"","_links":{"self":[{"href":"http:\/\/www.tyosuke20xx.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/25527","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.tyosuke20xx.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.tyosuke20xx.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.tyosuke20xx.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.tyosuke20xx.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=25527"}],"version-history":[{"count":1,"href":"http:\/\/www.tyosuke20xx.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/25527\/revisions"}],"predecessor-version":[{"id":25528,"href":"http:\/\/www.tyosuke20xx.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/25527\/revisions\/25528"}],"wp:attachment":[{"href":"http:\/\/www.tyosuke20xx.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=25527"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.tyosuke20xx.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=25527"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.tyosuke20xx.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=25527"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}