{"id":433,"date":"2016-12-20T01:34:22","date_gmt":"2016-12-19T16:34:22","guid":{"rendered":"http:\/\/okamurax.com\/?p=433"},"modified":"2016-12-23T10:04:29","modified_gmt":"2016-12-23T01:04:29","slug":"php-todo%e3%83%86%e3%82%b9%e3%83%88%e3%82%a2%e3%83%97%e3%83%aa","status":"publish","type":"post","link":"https:\/\/appbay.org\/?p=433","title":{"rendered":"PHP Todo\u30c6\u30b9\u30c8\u30a2\u30d7\u30ea"},"content":{"rendered":"<p>server.php <\/p>\n<pre class=\"lang:php decode:true \" >\r\n<?php\r\necho print_r($_POST);\r\n<\/pre>\n<p>post.php<\/p>\n<pre class=\"lang:php decode:true \" >\r\n<!DOCTYPE html>\r\n<html>\r\n<head>\r\n\t<script src=\"https:\/\/ajax.googleapis.com\/ajax\/libs\/jquery\/1.11.2\/jquery.min.js\"><\/script>\r\n\t<script src=\"https:\/\/ajax.googleapis.com\/ajax\/libs\/jqueryui\/1.11.3\/jquery-ui.min.js\"><\/script>\r\n\t<meta charset=\"UTF-8\">\r\n<\/head>\r\n<body>\r\n\t<button type=\"button\" class=\"btn\">submit<\/button>\r\n<script>\r\n$(function(){\r\n\t$(document).on('click','.btn',function(){\r\n\t\tif(confirm()){\r\n\t\t\tvar id = 'key';\r\n\t\t\t$.post(\r\n\t\t\t\t'server.php', \/\/\u30ea\u30af\u30a8\u30b9\u30c8URL\r\n\t\t\t\t{id: id}, \/\/\u9001\u4fe1\u30c7\u30fc\u30bf\r\n\t\t\t\t\r\n\t\t\t\t\/\/\u4e0a\u8a18\u3067Post\u3055\u308c\u308b\u5024\u306fid:key\u306b\u306a\u3063\u3066\u3044\u308b\u306e\u3067id\u5074\u306f\u5909\u6570\u306b\u3057\u3066\u3082\u5c55\u958b\u3055\u308c\u306a\u3044\u3002\r\n\t\t\t\t\/\/id\u5074\u306f\u30af\u30a9\u30fc\u30c6\u30fc\u30b7\u30e7\u30f3\u306e\u6709\u7121\u306f\u95a2\u4fc2\u306a\u3044\u3063\u307d\u3044\u3002\r\n\t\t\t\t\r\n\t\t\t\tfunction(rs){ \/\/\u901a\u4fe1\u6210\u529f\u6642\u306e\u30b3\u30fc\u30eb\u30d0\u30c3\u30af\r\n\t\t\t\t\talert(rs);\r\n\t\t\t\t}\r\n\t\t\t);\r\n\t\t}\r\n\t});\r\n});\r\n<\/script>\r\n<\/body>\r\n<\/html>\r\n\r\n\r\n<\/pre>\n<p>index.php<\/p>\n<pre class=\"lang:php decode:true \" >\r\n<?php\r\nrequire_once('config.php');\r\nrequire_once('functions.php');\r\n\r\n$db = con();\r\n\r\n$tasks = array();\r\n$sql = \"select * from tasks where type != 'deleted' order by seq\";\r\n\r\nforeach($db->query($sql) as $row){\r\n\tarray_push($tasks, $row);\r\n}\r\n\r\n?>\r\n<!DOCTYPE html>\r\n<html>\r\n<head>\r\n\t<script src=\"https:\/\/ajax.googleapis.com\/ajax\/libs\/jquery\/1.11.2\/jquery.min.js\"><\/script>\r\n\t<script src=\"https:\/\/ajax.googleapis.com\/ajax\/libs\/jqueryui\/1.11.3\/jquery-ui.min.js\"><\/script>\r\n\r\n\t<meta charset=\"UTF-8\">\r\n\t<title>Todo<\/title>\r\n\t<style>\r\n\t\t.delete, .drag, .edit{\r\n\t\t\tcursor: pointer;\r\n\t\t\tcolor: blue;\r\n\t\t}\r\n\t\t.done{\r\n\t\t\ttext-decoration: line-through;\r\n\t\t\tcolor: gray;\t\r\n\t\t}\r\n\t<\/style>\r\n<\/head>\r\n<body>\r\n<p>\r\n\t<input type=\"text\" id=\"title\">\r\n\t<input type=\"button\" id=\"addTask\" value=\"\u8ffd\u52a0\">\r\n<\/p>\r\n<ul id=\"tasks\"><!-- jQueryUI\u306esortable\u3092\u4f7f\u3046\u305f\u3081\u306eID -->\r\n\t\r\n\t<?php foreach ($tasks as $task): ?>\r\n\t<li id=\"task_<?php echo h($task['id']); ?>\" data-id=\"<?php echo h($task['id']); ?>\">\r\n\t\r\n\t\t<!-- \u30c1\u30a7\u30c3\u30af\u30dc\u30c3\u30af\u30b9 -->\r\n\t\t<input type=\"checkbox\" class=\"check\" <?php if($task['type'] == \"done\"){echo \"checked\";} ?>>\r\n\r\n\t\t<!-- \u672c\u6587 -->\r\n\t\t<span class=\"<?php echo h($task['type']); ?>\"><?php echo h($task['title']); ?><\/span>\r\n\t\t\t\r\n\t\t<!-- \u7de8\u96c6 -->\r\n\t\t<span <?php if($task['type'] == \"notyet\"){ echo 'class=\"edit\"'; } ?>>[\u7de8\u96c6]<\/span>\r\n\r\n\t\t<span class=\"delete\">[\u524a\u9664]<\/span>\r\n\t\t\t\r\n\t\t<span class=\"drag\">[drag]<\/span>\r\n\t<\/li>\r\n\t<?php endforeach; ?>\r\n<\/ul>\r\n<script>\r\n\t\r\n\/\/post\u304c\u30a8\u30e9\u30fc\u306e\u6642\u306e\u53cd\u5fdc\u304c\u5fc5\u8981\u3002\r\n\/\/\u524a\u9664\u3055\u308c\u305f\u3068\u304d\u306e\u53cd\u5fdc\u304c\u5fc5\u8981\u3002\r\n\r\n$(function(){\r\n\t\r\n\t$('#title').focus();\r\n\t\r\n\t$('#addTask').click(function(){\r\n\t\tvar title = $('#title').val();\r\n\t\t$.post('_ajax_add_task.php',{title: title},function(rs){\r\n\t\t\tvar e = $(\r\n\t\t\t\t'<li id=\"task_'+rs+'\" data-id=\"'+rs+'\">'+\r\n\t\t\t\t'<input type=\"checkbox\" class=\"check\">'+\r\n\t\t\t\t'<span><\/span>'+\r\n\t\t\t\t'<span class=\"edit\">[\u7de8\u96c6]<\/span>'+\r\n\t\t\t\t'<span class=\"delete\">[\u524a\u9664]<\/span>'+\r\n\t\t\t\t'<span class=\"drag\">[drag]<\/span>'+\r\n\t\t\t\t'<\/li>'\r\n\t\t\t);\r\n\t\t\t$('#tasks').append(e).find('li:last span:eq(0)').text(title);\r\n\t\t\t$('#title').val('').focus();\r\n\r\n\t\t});\r\n\t});\r\n\t\r\n\t\r\n\t$(document).on('click', '.edit', function(){\r\n\t\tvar id = $(this).parent().data('id');\r\n\t\tvar title = $(this).prev().text();\r\n\t\t$('#task_'+id)\r\n\t\t\t.empty()\r\n\t\t\t.append($('<input type=\"text\">').attr('value',title))\r\n\t\t\t.append('<input type=\"button\" value=\"\u66f4\u65b0\" class=\"update\">');\r\n\t\t$('#task_'+id+' input:eq(0)').focus();\r\n\t});\r\n\t\r\n\t$(document).on('click', '.update', function(){\r\n\t\tvar id = $(this).parent().data('id');\t\r\n\t\tvar title = $(this).prev().val();\r\n\t\t$.post('_ajax_update_task.php', {id: id, title: title}, function(rs){\r\n\t\t\tvar e = $(\r\n\t\t\t\t'<input type=\"checkbox\" class=\"check\">'+\r\n\t\t\t\t'<span><\/span>'+\r\n\t\t\t\t'<span class=\"edit\">[\u7de8\u96c6]<\/span>'+\r\n\t\t\t\t'<span class=\"delete\">[\u524a\u9664]<\/span>'+\r\n\t\t\t\t'<span class=\"drag\">[drag]<\/span>'\r\n\t\t\t);\r\n\t\t\t$('#task_'+id).empty().append(e).find('span:eq(0)').text(title);\r\n\t\t});\r\n\t});\r\n\t\r\n\t$('#tasks').sortable({\r\n\t\taxis: 'y', \/\/\u30c9\u30e9\u30c3\u30b0\u3067\u304d\u308b\u65b9\u5411\r\n\t\topacity: 0.2, \/\/\u30c9\u30e9\u30c3\u30b0\u4e2d\u306e\u900f\u660e\u5ea6\r\n\t\thandle: '.drag', \/\/\u30c9\u30e9\u30c3\u30b0\u306e\u5bfe\u8c61\r\n\t\tupdate: function(){ \/\/\u30c9\u30e9\u30c3\u30b0\u5f8c\u306e\u30b3\u30fc\u30eb\u30d0\u30c3\u30af\r\n\t\t\t$.post('_ajax_sort_task.php',{task: $(this).sortable('serialize')}); \/\/\u3069\u306e\u8981\u7d20\u304c\u4f55\u756a\u76ee\u304b\u306e\u6587\u5b57\u5217\r\n\t\t\t\/\/li\u8981\u7d20\u306eid\u304c\u6587\u5b57\u5217 + _ + \u6570\u5b57\u306b\u306a\u3063\u3066\u3044\u308b\u5fc5\u8981\u304c\u3042\u308b\u3002\r\n\t\t}\r\n\t});\r\n\t\r\n\t$(document).on('click', '.check', function(){\r\n\t\tvar id = $(this).parent().data('id'); \/\/data\u306f\u30d7\u30ed\u30d1\u30c6\u30a3\u3092\u53d6\u5f97\u3002\r\n\t\t\/\/.check\u306finput\u8981\u7d20\u3067\u89aa\u306fli\u8981\u7d20\u3002data\u30d7\u30ed\u30d1\u30c6\u30a3\u306fHTML\u306edata-*\u3092\u53d6\u5f97\u3059\u308b\u4ed5\u69d8\u3089\u3057\u3044\u3002\r\n\t\t\r\n\t\tvar title = $(this).next(); \/\/ <!-- \u672c\u6587 -->\u306e\u90e8\u5206\u306e\u30a8\u30ec\u30e1\u30f3\u30c8\u3092\u53d6\u5f97\u3057\u3066\u3044\u308b\u3002\r\n\t\t$.post('_ajax_check_task.php',{id: id},function(rs){\r\n\t\t\tif(title.hasClass('done')){\r\n\t\t\t\ttitle.removeClass('done').next().addClass('edit');\r\n\t\t\t}else{\r\n\t\t\t\ttitle.addClass('done').next().removeClass('edit');\r\n\t\t\t}\t\r\n\t\t});\r\n\t\r\n\t});\r\n\t\r\n\t$(document).on('click','.delete',function(){\r\n\t\tif(confirm('\u672c\u5f53\u306b\u524a\u9664\u3057\u307e\u3059\u304b\uff1f')){\r\n\t\t\tvar id = $(this).parent().data('id'); \/\/this\u306f<span>\u3092\u6307\u3059\u3002\u3053\u3053\u3067\u306fdata-id\u3092\u5229\u7528\u3057\u3066\u3044\u308b\u3002\r\n\t\t\t$.post('_ajax_delete_task.php',{id: id},function(rs){\r\n\t\t\t\t$('#task_'+id).fadeOut(800);\r\n\t\t\t});\r\n\t\t}\r\n\t});\r\n});\r\n<\/script>\r\n<\/body>\r\n\t\r\n<\/html>\r\n\r\n<\/pre>\n<p>functions.php<\/p>\n<pre class=\"lang:php decode:true \" >\r\n<?php\r\n\r\nfunction con(){\r\n\ttry{\r\n\t\treturn new PDO(DSN, DB_USER, DB_PASSWORD);\r\n\t}catch(PDOException $e){\r\n\t\techo $e->getMessage();\r\n\t\texit;\r\n\t}\r\n}\r\n\r\nfunction h($s){\r\n\treturn htmlspecialchars($s, ENT_QUOTES, \"UTF-8\");\r\n}\r\n<\/pre>\n<p>config.php<\/p>\n<pre class=\"lang:php decode:true \" >\r\n<?php\r\nerror_reporting(E_ALL &#038; ~E_NOTICE);\r\n\r\ndefine('DSN','mysql:host=localhost;dbname=todo_app;charset=utf8');\r\ndefine('DB_USER','user');\r\ndefine('DB_PASSWORD','1234');\r\n\r\n<\/pre>\n<p>commands.sql<\/p>\n<pre class=\"lang:php decode:true \" >\r\ncreate database todo_app;\r\ngrant all on todo_app.* to user@'localhost' identified by '1234';\r\nuse todo_app\r\ncreate table tasks(\r\n\tid int not null auto_increment primary key,\r\n\tseq int not null,\r\n\ttype enum('notyet','done','deleted') default 'notyet',\r\n\ttitle text,\r\n\tcreated datetime,\r\n\tmodified datetime,\r\n\tkey type(type),\r\n\tkey seq(seq)\r\n);\r\ninsert into tasks (seq,type,title,created,modified) values\r\n(1,'notyet','\u725b\u4e73\u8cb7\u3046',now(),now());\r\n\r\n<\/pre>\n<p>_ajax_update_task.php<\/p>\n<pre class=\"lang:php decode:true \" >\r\n<?php\r\nrequire_once('config.php');\r\nrequire_once('functions.php');\r\n\r\n$db = con();\r\n\r\n$sql = \"update tasks set title = :title, modified = now() where id = :id\";\r\n$st = $db->prepare($sql);\r\n$st->execute(array(\r\n\t\":id\" => (int)$_POST['id'],\r\n\t\":title\" => $_POST['title']\r\n));\r\n\r\n\r\n<\/pre>\n<p>_ajax_sort_task.php<\/p>\n<pre class=\"lang:php decode:true \" >\r\n<?php\r\nrequire_once('config.php');\r\nrequire_once('functions.php');\r\n\r\n$db = con();\r\n\r\n\/\/var_dump($_POST['task']);\r\n\/\/\u30c9\u30e9\u30c3\u30b0\u5f8c\u306bDevTools\u306e\u30cd\u30c3\u30c8\u30ef\u30fc\u30af\u30bf\u30d6\u304b\u3089\u3053\u306e\u30d5\u30a1\u30a4\u30eb\u3092\u9078\u629e\u3059\u308b\u3068\u30d5\u30a1\u30a4\u30eb\u306e\u51fa\u529b\u304c\u898b\u3089\u308c\u308b\u3002\r\n\r\nparse_str($_POST['task']); \/\/$task\u304c\u4f7f\u3048\u308b\u3088\u3046\u306b\u306a\u308b\u3002\r\nprint_r($task);\r\n\r\nforeach ($task as $key => $val ){\r\n\t$sql = \"update tasks set seq = :seq where id = :id\";\r\n\t$st = $db->prepare($sql);\r\n\t$st->execute(array(\r\n\t\t\":seq\" => $key,\r\n\t\t\":id\" => $val\r\n\t));\r\n}\r\n<\/pre>\n<p>_ajax_delete_task.php<\/p>\n<pre class=\"lang:php decode:true \" >\r\n<?php\r\nrequire_once('config.php');\r\nrequire_once('functions.php');\r\n\r\n$db = con();\r\n\r\n$sql = \"update tasks set type = 'deleted', modified = now() where id = :id\";\r\n$st = $db->prepare($sql);\r\n$st->execute(array(\":id\" => (int)$_POST['id']));\r\n\r\n\r\n<\/pre>\n<p>_ajax_check_task.php<\/p>\n<pre class=\"lang:php decode:true \" >\r\n<?php\r\nrequire_once('config.php');\r\nrequire_once('functions.php');\r\n\r\n$db = con();\r\n\r\n$sql = \"update tasks set type = if(type='done', 'notyet','done'), modified = now() where id = :id\";\r\n$st = $db->prepare($sql);\r\n$st->execute(array(\":id\" => (int)$_POST['id']));\r\n\r\n\r\n<\/pre>\n<p>_ajax_add_task.php<\/p>\n<pre class=\"lang:php decode:true \" >\r\n<?php\r\nrequire_once('config.php');\r\nrequire_once('functions.php');\r\n\r\n$db = con();\r\n\r\n$sql = \"select max(seq)+1 from tasks where type != 'deleted'\";\r\n$seq = $db->query($sql)->fetchColumn();\r\n\r\n$sql = \"insert into tasks (seq, title, created, modified) values (:seq, :title, now(), now())\";\r\n\r\n$st = $db->prepare($sql);\r\n\r\n$st->execute(array(\r\n\t\":seq\" => $seq,\r\n\t\":title\" => $_POST['title']\r\n));\r\n\r\necho $db->lastInsertId(); \/\/\u30b3\u30fc\u30eb\u30d0\u30c3\u30af\u306ers\u3067\u53d7\u53d6\u3063\u3066\u3044\u308b\u3002\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>server.php<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[3],"class_list":["post-433","post","type-post","status-publish","format-standard","hentry","category-1","tag-php"],"_links":{"self":[{"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/posts\/433","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=433"}],"version-history":[{"count":1,"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/posts\/433\/revisions"}],"predecessor-version":[{"id":434,"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/posts\/433\/revisions\/434"}],"wp:attachment":[{"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=433"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=433"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=433"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}