#目標
いいね機能の実装を試みています。
ログインユーザーのみいいね!することが可能で、1人1回、いいね!ボタンをクリックすると+1で値が追加され、再びクリックすると-1されるような処理をしていきたいです。
#現状(問題点)
現在、ログイン機能と投稿機能は実装できていて、いいね機能が途中段階です。
いいね機能に関しては、クリックすると+1されてpostsテーブルのlikes_countの値も同様に変化し、値を返してくれるというところまではできています。
ですが、ページをリロードするとボタンのクリックが消えて再びいいね!できてしまうという状況です。
具体的にどのようにすれば、目標のようないいね機能になるのでしょうか?
DB
1mysql> desc posts; 2+-------------+----------+------+-----+---------+-------------------+ 3| Field | Type | Null | Key | Default | Extra | 4+-------------+----------+------+-----+---------+-------------------+ 5| id | int(11) | NO | PRI | NULL | auto_increment | 6| file_name | text | NO | | NULL | | 7| extension | text | NO | | NULL | | 8| likes_count | int(11) | YES | | 0 | DEFAULT_GENERATED | 9| created | datetime | YES | | NULL | | 10| modified | datetime | YES | | NULL | | 11+-------------+----------+------+-----+---------+-------------------+ 12
php
1<?php 2 3ini_set('display_errors', 1); 4 5define('MAX_FILE_SIZE', 1 * 1024 * 1024); //1MB 6define('THUMBNAIL_WIDTH', 400); 7define('IMAGES_DIR', __DIR__ . '/images'); 8define('THUMBNAIL_DIR', __DIR__ . '/thumbs'); 9 10if (!function_exists('imagecreatetruecolor')) { 11 echo "GD not installed"; 12 exit; 13} 14 15 16require 'ImageUploader.php'; 17 18$uploader = new \MyApp\ImageUploader(); 19 20if($_SERVER['REQUEST_METHOD'] === 'POST') { 21 $uploader->upload(); 22} 23 24list($success, $error) = $uploader->getResults(); 25 26$images = $uploader->getImages(); 27 28 29require_once(__DIR__ . '/Login/config/config.php'); 30 31 ?> 32 33 34 <!DOCTYPE html> 35 <html lang="ja"> 36 <head> 37 <meta charset="utf-8"> 38 <title>**</title> 39 <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.4.1/css/all.css" integrity="sha384-5sAR7xN1Nv6T6+dT2mhtzEpVJvfS3NScPQTrOxhwjIuvcA67KV2R5Jz6kr4abQsz" crossorigin="anonymous"> 40 <link rel="stylesheet" href="styles.css"> 41 </head> 42 43 <body> 44 45 <header class="bg"> 46 <div class="container"> 47 <h1>****</h1> 48 </div> 49 <div class="logined"> 50 <p class="mypage"><a href="mypage.php"><i class="fas fa-user-circle"> My Page</i></a></p> 51 </div> 52 </header> 53 54 55 <!-- ** --> 56 <section class="feature"> 57 <div class="container"> 58 <h1><a href="">**</a></h1> 59 </div> 60 </section> 61 62 <section class="images"> 63 <ul class="img_row"> 64 <?php 65 $sql = "SELECT * FROM posts ORDER BY id desc;"; 66 $stmt = $pdo->prepare($sql); 67 $stmt -> execute(); 68 while ($row = $stmt -> fetch(\PDO::FETCH_ASSOC)): 69 ?> 70 <li> 71 <?php 72 echo ("<a href='/images/$row[file_name]'>"); 73 echo ("<img src='/thumbs/$row[file_name]' class='img' id ='$row[id]'></a>"); 74 echo ("<div class='heart icon' id='icon_$row[id]'></div>"); 75 echo ("<div class='likes_count' id='likes_count_$row[id]'> $row[likes_count] </div>"); 76 ?> 77 </li> 78 <?php endwhile; ?> 79 </ul> 80 </section> 81 82 <footer class="bg"> 83 <p>&***</p> 84 </footer> 85 86 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> 87 <script> 88 $(function() { 89 'use strict'; 90 91 $( '.icon' ).on( 'click', function() { 92 var $_t = $( this ); 93 $_t.css( 'color', 'red' ); 94 $.post( 'Countup.php', { 95 id: $_t.parent().find( 'img' ).attr( 'id' ) 96 }, function( data ) { 97 $_t.parent().find( '.likes_count' ).text( data ); 98 } ); 99 } ); 100 101 }); 102 103 104 </script> 105 </body> 106 </html> 107
php
1<?php 2 3require_once(__DIR__ . '/Login/config/config.php'); 4require_once(__DIR__ . '/Like.php'); 5 6$likeApp = new \MyApp\Like(); 7 8 9if ($_SERVER['REQUEST_METHOD'] === 'POST') { 10 try { 11 12 $res = $likeApp->post(); 13 echo $res; 14 15 } catch (Exception $e) { 16 header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error', true, 500); 17 echo $e->getMessage(); 18 exit; 19 } 20} 21 22 23?> 24
php
1<?php 2 3namespace MyApp; 4 5class Like { 6 7 private $pdo; 8 9 public function __construct() { 10 try { 11 $dsn = "mysql:dbhost=localhost;dbname=*****"; 12 $username = "***"; 13 $password = "***"; 14 $this->pdo = new \PDO($dsn, $username, $password); 15 $this->pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); 16 17 } catch (\PDOException $e) { 18 echo $e->getMessage(); 19 exit; 20 } 21 } 22 23 public function post() { 24 25 if (!isset($_POST['id'])) { 26 throw new \Exception('[update] id not set!'); 27 } 28 29 $id = $_POST['id']; 30 31 $this->pdo->beginTransaction(); 32 33 $sql = "UPDATE posts set likes_count = likes_count+1 where id = :id;"; 34 $stmt = $this->pdo->prepare($sql); 35 $stmt->bindValue(":id", $id, \PDO::PARAM_INT); 36 $stmt->execute(); 37 38 $sql = "SELECT likes_count from posts where id = :id;"; 39 $stmt = $this->pdo->prepare($sql); 40 $stmt->bindValue(":id", $id, \PDO::PARAM_INT); 41 $stmt->execute(); 42 $result = $stmt->fetch(\PDO::FETCH_ASSOC); 43 44 $this->pdo->commit(); 45 46 echo $result['likes_count']; 47 48 } 49} 50 51?> 52
回答4件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/11/29 09:09
2018/11/29 11:35