質問編集履歴
1
同じソースコードを二度表記していた
test
CHANGED
File without changes
|
test
CHANGED
@@ -174,9 +174,259 @@
|
|
174
174
|
|
175
175
|
```
|
176
176
|
|
177
|
+
|
178
|
+
|
179
|
+
|
180
|
+
|
177
|
-
```
|
181
|
+
```PHP
|
178
|
-
|
182
|
+
|
179
|
-
|
183
|
+
Todo.php
|
184
|
+
|
185
|
+
<?php
|
186
|
+
|
187
|
+
|
188
|
+
|
189
|
+
namespace MyApp;
|
190
|
+
|
191
|
+
|
192
|
+
|
193
|
+
class Todo {
|
194
|
+
|
195
|
+
private $_db;
|
196
|
+
|
197
|
+
|
198
|
+
|
199
|
+
public function __construct() {
|
200
|
+
|
201
|
+
try {
|
202
|
+
|
203
|
+
$this->_db = new \PDO(DSN, DB_USERNAME, DB_PASSWORD);
|
204
|
+
|
205
|
+
$this->_db->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
|
206
|
+
|
207
|
+
} catch (\PDOException $e) {
|
208
|
+
|
209
|
+
echo $e->getMessage();
|
210
|
+
|
211
|
+
exit;
|
212
|
+
|
213
|
+
}
|
214
|
+
|
215
|
+
}
|
216
|
+
|
217
|
+
|
218
|
+
|
219
|
+
public function getAll() {
|
220
|
+
|
221
|
+
$stmt = $this->_db->query("select * from todos order by id desc");
|
222
|
+
|
223
|
+
return $stmt->fetchAll(\PDO::FETCH_OBJ);
|
224
|
+
|
225
|
+
}
|
226
|
+
|
227
|
+
|
228
|
+
|
229
|
+
public function post() {
|
230
|
+
|
231
|
+
if (!isset($_POST['mode'])) {
|
232
|
+
|
233
|
+
throw new \Exception('mode not set!');
|
234
|
+
|
235
|
+
}
|
236
|
+
|
237
|
+
|
238
|
+
|
239
|
+
switch ($_POST['mode']) {
|
240
|
+
|
241
|
+
case 'update':
|
242
|
+
|
243
|
+
return $this->_update();
|
244
|
+
|
245
|
+
case 'create':
|
246
|
+
|
247
|
+
return $this->_create();
|
248
|
+
|
249
|
+
case 'delete':
|
250
|
+
|
251
|
+
return $this->_delete();
|
252
|
+
|
253
|
+
}
|
254
|
+
|
255
|
+
}
|
256
|
+
|
257
|
+
|
258
|
+
|
259
|
+
private function _update() {
|
260
|
+
|
261
|
+
if (!isset($_POST['id'])) {
|
262
|
+
|
263
|
+
throw new \Exception('[update] id not set!');
|
264
|
+
|
265
|
+
}
|
266
|
+
|
267
|
+
|
268
|
+
|
269
|
+
$this->_db->beginTransaction();
|
270
|
+
|
271
|
+
|
272
|
+
|
273
|
+
$sql = sprintf("update todos set state = (state + 1) %% 2 where id = %d", $_POST['id']);
|
274
|
+
|
275
|
+
$stmt = $this->_db->prepare($sql);
|
276
|
+
|
277
|
+
$stmt->execute();
|
278
|
+
|
279
|
+
|
280
|
+
|
281
|
+
$sql = sprintf("select state from todos where id = %d", $_POST['id']);
|
282
|
+
|
283
|
+
$stmt = $this ->_db->query($sql);
|
284
|
+
|
285
|
+
$state = $stmt->fetchColumn();
|
286
|
+
|
287
|
+
|
288
|
+
|
289
|
+
$this->_db->commit();
|
290
|
+
|
291
|
+
|
292
|
+
|
293
|
+
return [
|
294
|
+
|
295
|
+
'state' => $state
|
296
|
+
|
297
|
+
];
|
298
|
+
|
299
|
+
|
300
|
+
|
301
|
+
|
302
|
+
|
303
|
+
}
|
304
|
+
|
305
|
+
|
306
|
+
|
307
|
+
|
308
|
+
|
309
|
+
private function _create(){
|
310
|
+
|
311
|
+
|
312
|
+
|
313
|
+
}
|
314
|
+
|
315
|
+
|
316
|
+
|
317
|
+
private function _delete(){
|
318
|
+
|
319
|
+
|
320
|
+
|
321
|
+
}
|
322
|
+
|
323
|
+
|
324
|
+
|
325
|
+
}
|
326
|
+
|
327
|
+
|
328
|
+
|
329
|
+
```
|
330
|
+
|
331
|
+
|
332
|
+
|
333
|
+
```CSS
|
334
|
+
|
335
|
+
styles.css
|
336
|
+
|
337
|
+
body {
|
338
|
+
|
339
|
+
font-family: Arial, sans-serif;
|
340
|
+
|
341
|
+
}
|
342
|
+
|
343
|
+
|
344
|
+
|
345
|
+
#container {
|
346
|
+
|
347
|
+
width: 500px;
|
348
|
+
|
349
|
+
margin: 15px auto;
|
350
|
+
|
351
|
+
}
|
352
|
+
|
353
|
+
|
354
|
+
|
355
|
+
h1 {
|
356
|
+
|
357
|
+
font-size: 18px;
|
358
|
+
|
359
|
+
text-align: center;
|
360
|
+
|
361
|
+
margin-bottom: 10px;
|
362
|
+
|
363
|
+
}
|
364
|
+
|
365
|
+
|
366
|
+
|
367
|
+
ul {
|
368
|
+
|
369
|
+
list-style: none;
|
370
|
+
|
371
|
+
padding: 0;
|
372
|
+
|
373
|
+
margin: 0;
|
374
|
+
|
375
|
+
}
|
376
|
+
|
377
|
+
|
378
|
+
|
379
|
+
ul >li {
|
380
|
+
|
381
|
+
padding-bottom: 6px;
|
382
|
+
|
383
|
+
}
|
384
|
+
|
385
|
+
|
386
|
+
|
387
|
+
#new_todo {
|
388
|
+
|
389
|
+
padding: 8px;
|
390
|
+
|
391
|
+
border-radius: 3px;
|
392
|
+
|
393
|
+
width: 490px;
|
394
|
+
|
395
|
+
margin: 0 auto;
|
396
|
+
|
397
|
+
font-size: 16px;
|
398
|
+
|
399
|
+
}
|
400
|
+
|
401
|
+
|
402
|
+
|
403
|
+
.delete_todo {
|
404
|
+
|
405
|
+
float: right;
|
406
|
+
|
407
|
+
display: inline-block;
|
408
|
+
|
409
|
+
color: #ccc;
|
410
|
+
|
411
|
+
cursor: pointer;
|
412
|
+
|
413
|
+
}
|
414
|
+
|
415
|
+
|
416
|
+
|
417
|
+
.done {
|
418
|
+
|
419
|
+
text-decoration: line-through;
|
420
|
+
|
421
|
+
}
|
422
|
+
|
423
|
+
|
424
|
+
|
425
|
+
```
|
426
|
+
|
427
|
+
|
428
|
+
|
429
|
+
```jQuery
|
180
430
|
|
181
431
|
$(function () {
|
182
432
|
|
@@ -226,304 +476,6 @@
|
|
226
476
|
|
227
477
|
|
228
478
|
|
229
|
-
```PHP
|
230
|
-
|
231
|
-
Todo.php
|
232
|
-
|
233
|
-
<?php
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
namespace MyApp;
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
class Todo {
|
242
|
-
|
243
|
-
private $_db;
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
public function __construct() {
|
248
|
-
|
249
|
-
try {
|
250
|
-
|
251
|
-
$this->_db = new \PDO(DSN, DB_USERNAME, DB_PASSWORD);
|
252
|
-
|
253
|
-
$this->_db->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
|
254
|
-
|
255
|
-
} catch (\PDOException $e) {
|
256
|
-
|
257
|
-
echo $e->getMessage();
|
258
|
-
|
259
|
-
exit;
|
260
|
-
|
261
|
-
}
|
262
|
-
|
263
|
-
}
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
public function getAll() {
|
268
|
-
|
269
|
-
$stmt = $this->_db->query("select * from todos order by id desc");
|
270
|
-
|
271
|
-
return $stmt->fetchAll(\PDO::FETCH_OBJ);
|
272
|
-
|
273
|
-
}
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
public function post() {
|
278
|
-
|
279
|
-
if (!isset($_POST['mode'])) {
|
280
|
-
|
281
|
-
throw new \Exception('mode not set!');
|
282
|
-
|
283
|
-
}
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
switch ($_POST['mode']) {
|
288
|
-
|
289
|
-
case 'update':
|
290
|
-
|
291
|
-
return $this->_update();
|
292
|
-
|
293
|
-
case 'create':
|
294
|
-
|
295
|
-
return $this->_create();
|
296
|
-
|
297
|
-
case 'delete':
|
298
|
-
|
299
|
-
return $this->_delete();
|
300
|
-
|
301
|
-
}
|
302
|
-
|
303
|
-
}
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
private function _update() {
|
308
|
-
|
309
|
-
if (!isset($_POST['id'])) {
|
310
|
-
|
311
|
-
throw new \Exception('[update] id not set!');
|
312
|
-
|
313
|
-
}
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
$this->_db->beginTransaction();
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
$sql = sprintf("update todos set state = (state + 1) %% 2 where id = %d", $_POST['id']);
|
322
|
-
|
323
|
-
$stmt = $this->_db->prepare($sql);
|
324
|
-
|
325
|
-
$stmt->execute();
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
$sql = sprintf("select state from todos where id = %d", $_POST['id']);
|
330
|
-
|
331
|
-
$stmt = $this ->_db->query($sql);
|
332
|
-
|
333
|
-
$state = $stmt->fetchColumn();
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
$this->_db->commit();
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
return [
|
342
|
-
|
343
|
-
'state' => $state
|
344
|
-
|
345
|
-
];
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
}
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
|
356
|
-
|
357
|
-
private function _create(){
|
358
|
-
|
359
|
-
|
360
|
-
|
361
|
-
}
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
private function _delete(){
|
366
|
-
|
367
|
-
|
368
|
-
|
369
|
-
}
|
370
|
-
|
371
|
-
|
372
|
-
|
373
|
-
}
|
374
|
-
|
375
|
-
|
376
|
-
|
377
|
-
```
|
378
|
-
|
379
|
-
|
380
|
-
|
381
|
-
```CSS
|
382
|
-
|
383
|
-
styles.css
|
384
|
-
|
385
|
-
body {
|
386
|
-
|
387
|
-
font-family: Arial, sans-serif;
|
388
|
-
|
389
|
-
}
|
390
|
-
|
391
|
-
|
392
|
-
|
393
|
-
#container {
|
394
|
-
|
395
|
-
width: 500px;
|
396
|
-
|
397
|
-
margin: 15px auto;
|
398
|
-
|
399
|
-
}
|
400
|
-
|
401
|
-
|
402
|
-
|
403
|
-
h1 {
|
404
|
-
|
405
|
-
font-size: 18px;
|
406
|
-
|
407
|
-
text-align: center;
|
408
|
-
|
409
|
-
margin-bottom: 10px;
|
410
|
-
|
411
|
-
}
|
412
|
-
|
413
|
-
|
414
|
-
|
415
|
-
ul {
|
416
|
-
|
417
|
-
list-style: none;
|
418
|
-
|
419
|
-
padding: 0;
|
420
|
-
|
421
|
-
margin: 0;
|
422
|
-
|
423
|
-
}
|
424
|
-
|
425
|
-
|
426
|
-
|
427
|
-
ul >li {
|
428
|
-
|
429
|
-
padding-bottom: 6px;
|
430
|
-
|
431
|
-
}
|
432
|
-
|
433
|
-
|
434
|
-
|
435
|
-
#new_todo {
|
436
|
-
|
437
|
-
padding: 8px;
|
438
|
-
|
439
|
-
border-radius: 3px;
|
440
|
-
|
441
|
-
width: 490px;
|
442
|
-
|
443
|
-
margin: 0 auto;
|
444
|
-
|
445
|
-
font-size: 16px;
|
446
|
-
|
447
|
-
}
|
448
|
-
|
449
|
-
|
450
|
-
|
451
|
-
.delete_todo {
|
452
|
-
|
453
|
-
float: right;
|
454
|
-
|
455
|
-
display: inline-block;
|
456
|
-
|
457
|
-
color: #ccc;
|
458
|
-
|
459
|
-
cursor: pointer;
|
460
|
-
|
461
|
-
}
|
462
|
-
|
463
|
-
|
464
|
-
|
465
|
-
.done {
|
466
|
-
|
467
|
-
text-decoration: line-through;
|
468
|
-
|
469
|
-
}
|
470
|
-
|
471
|
-
|
472
|
-
|
473
|
-
```
|
474
|
-
|
475
|
-
|
476
|
-
|
477
|
-
```jQuery
|
478
|
-
|
479
|
-
$(function () {
|
480
|
-
|
481
|
-
'use strict';
|
482
|
-
|
483
|
-
|
484
|
-
|
485
|
-
// update
|
486
|
-
|
487
|
-
$('#todos').on('click', '.update_todo', function () {
|
488
|
-
|
489
|
-
// id
|
490
|
-
|
491
|
-
var id = $(this).parents('li').data('id');
|
492
|
-
|
493
|
-
// ajax
|
494
|
-
|
495
|
-
$.post('_ajax.php', {
|
496
|
-
|
497
|
-
id: id,
|
498
|
-
|
499
|
-
mode: 'update'
|
500
|
-
|
501
|
-
}, function(res) {
|
502
|
-
|
503
|
-
if (res.state === '1') {
|
504
|
-
|
505
|
-
$('#todo_' + id).find('.todo_title').addClass('done');
|
506
|
-
|
507
|
-
} else {
|
508
|
-
|
509
|
-
$('#todo_' + id).find('.todo_title').removeClass('done');
|
510
|
-
|
511
|
-
}
|
512
|
-
|
513
|
-
})
|
514
|
-
|
515
|
-
});
|
516
|
-
|
517
|
-
|
518
|
-
|
519
|
-
});
|
520
|
-
|
521
|
-
|
522
|
-
|
523
|
-
```
|
524
|
-
|
525
|
-
|
526
|
-
|
527
479
|
|
528
480
|
|
529
481
|
### 試したこと
|