LoginSignup
10
12

More than 3 years have passed since last update.

【PHP】いいね機能実装

Posted at

PHPについて学習内容を備忘録としてまとめます。

いいね機能

PHPでajax処理を用いたいいね機能を実装しましたので、ここに実装方法をまとめます。
いいね機能とはFacebookやツイッターにあるお気に入り機能を指します。

いいねボタンの実装

まずはいいねボタンを実装します。
ボタンを配置する場所は適切なところに設置してください。

#user_disp.php

//ユーザーIDと投稿IDを元にいいね値の重複チェックを行っています
function check_favolite_duplicate($user_id,$post_id){
    $dsn='mysql:dbname=db;host=localhost;charset=utf8';
    $user='root';
    $password='';
    $dbh=new PDO($dsn,$user,$password);
    $sql = "SELECT *
            FROM favorite
            WHERE user_id = :user_id AND post_id = :post_id";
    $stmt = $dbh->prepare($sql);
    $stmt->execute(array(':user_id' => $user_id ,
                         ':post_id' => $post_id));
    $favorite = $stmt->fetch();
    return $favorite;
}

<form class="favorite_count" action="#" method="post">
        <input type="hidden" name="post_id">
        <button type="button" name="favorite" class="favorite_btn">
        <?php if (!check_favolite_duplicate($_SESSION['user_id'],$post_id)): ?>
          いいね
        <?php else: ?>
          いいね解除
        <?php endif; ?>
        </button>
</form>

上記ではいいねボタンをクリックした際に、check_favolite_duplicate関数ですでに投稿をお気に入りしているかを判断し、
ボタンをいいねいいね解除に切り替えています。

当然このままではajax処理は行われないため、JavaScriptを利用していきます。
jsファイルを作成し、下記を追加します。

jsファイルを作成

//URLから引数に入っている値を渡す処理
function get_param(name, url) {
    if (!url) url = window.location.href;
    name = name.replace(/[\[\]]/g, "\\$&");
    var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
        results = regex.exec(url);
    if (!results) return null;
    if (!results[2]) return false;
    return decodeURIComponent(results[2].replace(/\+/g, " "));
}


$(document).on('click','.favorite_btn',function(e){
    e.stopPropagation();
    var $this = $(this),
        page_id = get_param('page_id'),
        post_id = get_param('procode');
    $.ajax({
        type: 'POST',
        url: 'ajax_post_favorite_process.php',
        dataType: 'json',
        data: { page_id: page_id,
                post_id: post_id}
    }).done(function(data){
        location.reload();
    }).fail(function() {
      location.reload();
    });
  });

上記処理ではクラス名がfavorite_btnであるボタンをクリックした際に、
ajax_post_favorite_process.phppage_idpost_idを渡して処理を進めています。

get_paramとはURLから引数に入っている値を取り出すことができます。
この関数からpage_id(ユーザーID)とpost_id(投稿ID)を受け取っています。

ajax処理の追加

ではajax_post_favorite_process.phpで進めている処理を見ていきます。

<script src=" https://code.jquery.com/jquery-3.4.1.min.js "></script>
<script src="../js/user_page.js"></script>
<?php
session_start();
session_regenerate_id(true);
require_once('config.php');

function check_favolite_duplicate($user_id,$post_id){
    $dsn='mysql:dbname=db;host=localhost;charset=utf8';
    $user='root';
    $password='';
    $dbh=new PDO($dsn,$user,$password);
    $sql = "SELECT *
            FROM favorite
            WHERE user_id = :user_id AND post_id = :post_id";
    $stmt = $dbh->prepare($sql);
    $stmt->execute(array(':user_id' => $user_id ,
                         ':post_id' => $post_id));
    $favorite = $stmt->fetch();
    return $favorite;
}

if(isset($_POST)){

  $current_user = get_user($_SESSION['user_id']);
  $page_id = $_POST['page_id'];
  $post_id = $_POST['post_id'];

  $profile_user_id = $_POST['page_id'] ?: $current_user['user_id'];

  //既に登録されているか確認
  if(check_favolite_duplicate($current_user['id'],$post_id)){
    $action = '解除';
    $sql = "DELETE
            FROM favorite
            WHERE :user_id = user_id AND :post_id = post_id";
  }else{
    $action = '登録';
    $sql = "INSERT INTO favorite(user_id,post_id)
            VALUES(:user_id,:post_id)";
  }

  try{
    $dsn='mysql:dbname=shop;host=localhost;charset=utf8';
    $user='root';
    $password='';
    $dbh=new PDO($dsn,$user,$password);
    $stmt = $dbh->prepare($sql);
    $stmt->execute(array(':user_id' => $current_user['code'] , ':post_id' => $post_id));

  } catch (\Exception $e) {
    error_log('エラー発生:' . $e->getMessage());
    set_flash('error',ERR_MSG1);
    echo json_encode("error");
  }
}

check_favolite_duplicate関数で現在のユーザーIDと投稿IDを取得しデータベースに組み合わせが重複していないか確認を取り、
重複していた場合にいいねを解除しています。
重複していなかった場合はfavoriteテーブルにuser_idとpost_idを追加します。

これで下記の画面のようにいいねボタンが実装され、テーブルにもカラムが追加されていると思います。

image.png
image.png

↓いいねボタンをクリック
image.png
image.png
上記のような画面になりデータベースが更新されれば成功です。
こちらはユーザーIDが7、投稿IDが13で実行しています。

参考URL

10
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
12