본문 바로가기
PHP 박살내기/php ajax json

php 좋아요 기능& 알림기능 구현 2 (좋아요 총개수 불러오기& 글 삽입 기능)

by 인생여희 2017. 9. 19.
반응형

php 좋아요 기능& 알림기능 구현 2 (좋아요 총개수 불러오기& 글 삽입 기능)



#화면


#경로


index.php

<?php

include("database_connection.php");
if(!isset($_SESSION["user_id"]))
{

header("location:login.php");
}

?>



<!DOCTYPE html>
<html>
<head>
<title>php 좋아요 기능과 알림기능</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
</head>
<body>
<br />



<div class="container">
<h2 align="center">php 좋아요 기능과 알림기능</h2>
<br />
<div align="right">
<a href="logout.php">로그아웃</a>
</div>
<br />

<!--담벼락 글 작성란 -->
<form method="post" id="form_wall">
<textarea name="content" id="content" class="form-control" placeholder="무슨 생각을 하고계세요?"></textarea>


</br>
<!--담벼락 작성 버튼 -->
<div align="right">
<input type="submit" name="submit" id="submit" class="btn btn-primary btn-sm" value="Post" />
</div>
</form>

<br />
<br />


<br />
<br />
<h4>최근 글</h4>
<br /> <!--최근 글을 ajax로 가져와서 붙일 거다 -->
<div id="website_stuff"></div>



</div>
</body>
</html>


<script>
$(document).ready(function(){

load_stuff();

function load_stuff()
{
$.ajax({
url:"load_stuff.php",
method:"POST",
success:function(data)
{
$('#website_stuff').html(data);
}
});
}

$('#form_wall').on('submit', function(event){
event.preventDefault();



if($.trim($('#content').val()).length == 0)
{
alert("글을 입력해 주세요");
return false;
}else
{
var form_data = $(this).serialize();

$.ajax({
url:"insert.php",
method:"POST",
data:new FormData(this),
contentType:false,
processData:false,
success:function(data){

if(data == 'done')
{
alert(data);
$('#form_wall')[0].reset();
load_stuff();
}else{
alert(data);
$('#form_wall')[0].reset();
load_stuff();
}
}
});
}

});


});

</script>

login.php

<?php

include("database_connection.php");

$msg = '';

if(isset($_POST["login"]))
{
if(empty($_POST["user_email"]) || empty($_POST["user_password"]))
{
$msg = '<label>빈칸을 채워주세요</label>';
}
else
{
$query = "select * from user_details where user_email = :user_email";
$statment = $connect ->prepare($query);
$statment -> execute(
array('user_email' => $_POST["user_email"])
);
$count = $statment -> rowCount();

if($count > 0 )
{
$result =$statment ->fetchAll();
foreach($result as $row)
{
if(password_verify($_POST["user_password"],$row["user_password"]))
{
$_SESSION['user_id'] = $row['user_id'];
$_SESSION['user_name'] = $row['user_name'];
header("location:index.php");
}else
{
$msg = '<label>잘못된 비밀번호 입니다</label>';
}

}
}
else
{
$msg = '<label>잘못된 이메일 주소 입니다</label>';
}


}

}

?>

<!DOCTYPE html>
<html>
<head>
<title>php 좋아요 기능과 알림기능</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
</head>
<body>
<br />



<div class="container">
<h2 align="center">php 좋아요 기능과 알림기능</h2>
<br />
<div class="panel panel-default">
<div class="panel-heading">로그인</div>
<div class="panel-body">

<!-- 로그인 폼 -->
<form method = "post">
<span class="text-danger"><?php echo $msg; ?></span>
<div class="form-group">
<label>이메일</label>
<input type="text" name="user_email" id="user_email" class="form-control" />
</div>

<div class="form-group">
<label>비밀번호</label>
<input type="password" name="user_password" id="user_password" class="form-control" />
</div>

<div class="form-group">
<input type="submit" name="login" id="login" class="btn btn-info" value="로그인"/>
</div>
</form>
</div>
</div>
</div>
</div>
</body>
</html>




logout.php

<?php

session_start();
session_destroy();
header("location:login.php");

?>


load_stuff.php

<?php
include("database_connection.php");
include("function.php");
if(isset($_SESSION["user_id"]))
{
$output = '';
$query = "
SELECT content.content_id, content.user_id, content.description,
user_details.user_name, user_details.user_image
from content
INNER JOIN user_details
ON user_details.user_id = content.user_id
ORDER BY content_id DESC
";

$statement = $connect -> prepare($query);
$statement -> execute();
$result = $statement ->fetchAll();
$total_rows = $statement ->rowCount();
if($total_rows > 0 )
{
foreach($result as $row)
{
$like_button = '';
//행이 없으면, 즉 좋아요 버튼이 안눌렸으면
if(!is_user_has_already_like_content($connect, $_SESSION["user_id"], $row["content_id"]))
{
//좋아요 버튼 생성
$like_button = '
<button type="button" name = "like_button"
class="btn btn-info btn-xs like_button" data-content_id= "'.$row["content_id"].'">
좋아요 </button>';
}

//한 게시물에 좋아요 몇개?
$count_like = count_content_like($connect, $row["content_id"]);

$output .= '
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">
<img src="images/'.$row["user_image"].'" class="img-thumbnail"
width="40" height="40" /> by '.$row["user_name"].'
</h3>

<button type="button" name = "total-like" id="total-like"
class="btn btn-warning btn-xs"> '.$count_like.' 좋아요 </button>
</div>
<div class="panel-body">
'.$row["description"].'
</div>

<div class="panel-footer" align="right">
'.$like_button.'
</div>
</div>
';
}
}
else
{
$output = '게시물이 없습니다';
}

echo $output;
}
?>


insert.php

<?php
//insert.php

include('database_connection.php');



if(isset($_POST["content"]))
{
$query = "
INSERT INTO content
(user_id, description)
VALUES(:user_id, :description)
";
$statement = $connect->prepare($query);
$statement->execute(
array(
':user_id' => $_SESSION['user_id'],
':description' => $_POST["content"]
)
);


$result = $statement->fetchAll();


if(isset($result))
{
echo 'done';
}
}

?>


function.php

<?php

//한 유저가 한 게시물에 좋아요를 눌렀는지 안눌렀는지 체크해주는 함수
//user_id 와 특정 content_id 로 조회 해서 값이 있으면 좋아요 버튼을 누른것
//없으면 안누른것
function is_user_has_already_like_content($connect, $user_id, $content_id)
{

$query = "
SELECT * FROM user_content_like
WHERE content_id = :content_id
AND user_id = :user_id";

$statement = $connect -> prepare($query);

$statement -> execute(
array(':content_id' => $content_id,
':user_id' => $user_id
)
);

$total_rows = $statement -> rowCount();

if($total_rows > 0)
{
return true;
}
else
{
return false;
}

}

//게시물이 좋아요 몇 번을 받았는지 반환해 주는 함수
function count_content_like($connect, $content_id)
{

$query = "
SELECT * FROM user_content_like
WHERE content_id = :content_id";

$statement = $connect -> prepare($query);
$statement -> execute(
array(':content_id' => $content_id)
);

$total_rows = $statement -> rowCount();
return $total_rows;
}


?>


database_connection.php

<?php

$connect = new PDO("mysql:host=localhost; dbname=db이름","root","비번");

session_start();
?>


반응형

댓글