質問編集履歴
2
いいね機能部分のファイルの追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -389,3 +389,155 @@
|
|
389
389
|
</div>
|
390
390
|
|
391
391
|
```
|
392
|
+
|
393
|
+
|
394
|
+
|
395
|
+
### ajax.php
|
396
|
+
|
397
|
+
```
|
398
|
+
|
399
|
+
<?php
|
400
|
+
|
401
|
+
session_start();
|
402
|
+
|
403
|
+
|
404
|
+
|
405
|
+
require_once 'function.php';
|
406
|
+
|
407
|
+
|
408
|
+
|
409
|
+
// postがある場合
|
410
|
+
|
411
|
+
if(isset($_POST['postId'])){
|
412
|
+
|
413
|
+
$p_id = $_POST['postId'];
|
414
|
+
|
415
|
+
|
416
|
+
|
417
|
+
try{
|
418
|
+
|
419
|
+
//DB接続
|
420
|
+
|
421
|
+
$dbh = dbConnect();
|
422
|
+
|
423
|
+
// Likesテーブルから投稿IDとユーザーIDが一致したレコードを取得するSQL文
|
424
|
+
|
425
|
+
$sql = 'SELECT * FROM Likes WHERE post_id = :p_id AND member_id = :u_id';
|
426
|
+
|
427
|
+
$data = array(':p_id' => $p_id, 'u_id' => $_SESSION['member_id']);
|
428
|
+
|
429
|
+
// クエリ実行
|
430
|
+
|
431
|
+
$stmt = queryPost($dbh, $sql, $data);
|
432
|
+
|
433
|
+
$resultCount = $stmt->rowCount();
|
434
|
+
|
435
|
+
// レコードが1件でもある場合
|
436
|
+
|
437
|
+
if(!empty($resultCount)){
|
438
|
+
|
439
|
+
// レコードを削除する
|
440
|
+
|
441
|
+
$sql = 'DELETE FROM Likes WHERE post_id = :p_id AND member_id = :u_id';
|
442
|
+
|
443
|
+
$data = array(':p_id' => $p_id, ':u_id' => $_SESSION['member_id']);
|
444
|
+
|
445
|
+
// クエリ実行
|
446
|
+
|
447
|
+
$stmt = queryPost($dbh, $sql, $data);
|
448
|
+
|
449
|
+
echo count(getGood($p_id));
|
450
|
+
|
451
|
+
}else{
|
452
|
+
|
453
|
+
// レコードを挿入する
|
454
|
+
|
455
|
+
$sql = 'INSERT INTO Likes (post_id, member_id, like_created_at) VALUES (:p_id, :u_id, :date)';
|
456
|
+
|
457
|
+
$data = array(':p_id' => $p_id, ':u_id' => $_SESSION['member_id'], ':date' => date('Y-m-d H:i:s'));
|
458
|
+
|
459
|
+
// クエリ実行
|
460
|
+
|
461
|
+
$stmt = queryPost($dbh, $sql, $data);
|
462
|
+
|
463
|
+
echo count(getGood($p_id));
|
464
|
+
|
465
|
+
var_dump(count(getGood($p_id)));
|
466
|
+
|
467
|
+
}
|
468
|
+
|
469
|
+
}catch(Exception $e){
|
470
|
+
|
471
|
+
error_log('エラー発生:'.$e->getMessage());
|
472
|
+
|
473
|
+
}
|
474
|
+
|
475
|
+
}
|
476
|
+
|
477
|
+
|
478
|
+
|
479
|
+
?>
|
480
|
+
|
481
|
+
```
|
482
|
+
|
483
|
+
### likes_test.js
|
484
|
+
|
485
|
+
```
|
486
|
+
|
487
|
+
$(function(){
|
488
|
+
|
489
|
+
var $good = $('.btn-good'), //いいねボタンセレクタ
|
490
|
+
|
491
|
+
goodPostId; //投稿ID
|
492
|
+
|
493
|
+
$good.on('click',function(e){
|
494
|
+
|
495
|
+
e.stopPropagation();
|
496
|
+
|
497
|
+
var $this = $(this);
|
498
|
+
|
499
|
+
//カスタム属性(postid)に格納された投稿ID取得
|
500
|
+
|
501
|
+
goodPostId = $this.parents('.post').data('postid');
|
502
|
+
|
503
|
+
$.ajax({
|
504
|
+
|
505
|
+
type: 'POST',
|
506
|
+
|
507
|
+
url: 'Likes/ajax.php', //post送信を受けとるphpファイル
|
508
|
+
|
509
|
+
data: { postId: goodPostId} //{キー:投稿ID}
|
510
|
+
|
511
|
+
}).done(function(data){
|
512
|
+
|
513
|
+
console.log('Ajax Success');
|
514
|
+
|
515
|
+
|
516
|
+
|
517
|
+
// いいねの総数を表示
|
518
|
+
|
519
|
+
$this.children('span').html(data);
|
520
|
+
|
521
|
+
// いいね取り消しのスタイル
|
522
|
+
|
523
|
+
$this.children('i').toggleClass('far'); //空洞ハート
|
524
|
+
|
525
|
+
// いいね押した時のスタイル
|
526
|
+
|
527
|
+
$this.children('i').toggleClass('fas'); //塗りつぶしハート
|
528
|
+
|
529
|
+
$this.children('i').toggleClass('active');
|
530
|
+
|
531
|
+
$this.toggleClass('active');
|
532
|
+
|
533
|
+
}).fail(function(msg) {
|
534
|
+
|
535
|
+
console.log('Ajax Error');
|
536
|
+
|
537
|
+
});
|
538
|
+
|
539
|
+
});
|
540
|
+
|
541
|
+
});
|
542
|
+
|
543
|
+
```
|
1
データベースの表示をマークダウン記法にしました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -60,6 +60,8 @@
|
|
60
60
|
|
61
61
|
テーブル Members
|
62
62
|
|
63
|
+
```
|
64
|
+
|
63
65
|
+-------------------+--------------+------+-----+-------------------+-----------------------------------------------+
|
64
66
|
|
65
67
|
| Field | Type | Null | Key | Default | Extra |
|
@@ -82,10 +84,14 @@
|
|
82
84
|
|
83
85
|
+-------------------+--------------+------+-----+-------------------+-----------------------------------------------+
|
84
86
|
|
87
|
+
```
|
88
|
+
|
85
89
|
|
86
90
|
|
87
91
|
テーブル Post
|
88
92
|
|
93
|
+
```
|
94
|
+
|
89
95
|
+-----------+--------------+------+-----+-------------------+-------------------+
|
90
96
|
|
91
97
|
| Field | Type | Null | Key | Default | Extra |
|
@@ -104,8 +110,12 @@
|
|
104
110
|
|
105
111
|
|
106
112
|
|
113
|
+
```
|
114
|
+
|
107
115
|
テーブル Likes
|
108
116
|
|
117
|
+
```
|
118
|
+
|
109
119
|
|
110
120
|
|
111
121
|
+-----------------+----------+------+-----+-------------------+-----------------------------------------------+
|
@@ -126,7 +136,7 @@
|
|
126
136
|
|
127
137
|
+-----------------+----------+------+-----+-------------------+-----------------------------------------------+
|
128
138
|
|
129
|
-
|
139
|
+
```
|
130
140
|
|
131
141
|
|
132
142
|
|