Created
January 27, 2021 13:19
-
-
Save ayaysir/19226dfe068f48a69a2a6b472cb48be3 to your computer and use it in GitHub Desktop.
좋아요 기능이 있는 게시판 http://yoonbumtae.com/phpex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
header('Content-Type: text/html; charset=utf-8'); | |
$seq = $_POST['seq']; | |
include "initializeDB.php"; | |
$sql = "delete from messages where seq='$seq'"; | |
if($mysqli->query($sql)) { | |
echo "해당 글이 정상적으로 삭제되었습니다."; | |
} else { | |
echo "삭제에 실패했습니다."; | |
} | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="ko"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>PHP Board</title> | |
<link rel="stylesheet" type="text/css" href="https://bootswatch.com/4/cerulean/bootstrap.min.css"> | |
<script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script> | |
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script> | |
<style> | |
body{ | |
padding: 10px; | |
} | |
table{ | |
margin-top: 5px; | |
} | |
button{ | |
width: 100%; | |
} | |
.btn-like { | |
display: none; | |
border: none; | |
background-color: inherit; | |
} | |
.btn-like .heart-shape { | |
display: inline; | |
color: red; | |
} | |
</style> | |
<script type="text/javascript" src="phpex.js"></script> | |
</head> | |
<body> | |
<div class=container> | |
<div class=row> | |
<div class=col-12> | |
<h2>PHP Board</h2> | |
</div> | |
</div> | |
<div class=row> | |
<div class=col-3> | |
<button type="button" id=insertBtn class="btn btn-primary">Insert</button> | |
</div> | |
<div class=col-3> | |
<button type="button" id=selectBtn class="btn btn-success">Select</button> | |
</div> | |
<div class=col-3> | |
<button type="button" id=updateBtn class="btn btn-warning">Update</button> | |
</div> | |
<div class=col-3> | |
<button type="button" id=deleteBtn class="btn btn-danger">Delete</button> | |
</div> | |
</div> | |
<div class=row> | |
<div class=col-12> | |
<?php | |
include "initializeDB.php"; | |
/* if($mysqli){echo "MySQL 접속 성공";} | |
else{echo "MySQL 접속 실패";} */ | |
$sql = 'SELECT * FROM messages order by seq desc'; | |
$res = $mysqli->query($sql); | |
// echo '<br>num_rows: count is '.$res->num_rows; | |
// echo '<br>field_count: count is '.$res->field_count; | |
$chkboxIndex = 1; | |
?> | |
<form id=tableFrm> | |
<table class="table table-hover"> | |
<thead> | |
<tr> | |
<th scope="col">No.</th> | |
<th scope="col">작성자</th> | |
<th scope="col">메시지</th> | |
<th scope="col">작성(수정)일자</th> | |
<th scope="col">좋아요</th> | |
</tr> | |
</thead> | |
<tbody> | |
<?php while($row = mysqli_fetch_array($res)) { ?> | |
<tr> | |
<?php | |
$seq = $row['seq']; | |
echo "<th scope='row'>"; | |
echo "<span id='rownum".$seq."' class='rowseq'>".$seq."</span>"; | |
echo "<input type=radio id=radio".$seq." value=".$seq." name=seq>"; | |
echo "</th>"; | |
echo "<td id=writerTd".$seq.">".$row['writer']."</td>"; | |
echo "<td id=messageTd".$seq.">".$row['message']."</td>"; | |
echo "<td id=wdateTd".$seq.">".$row['wdate']."</td>"; | |
echo '<td class="like-container"><button type="button" class="btn-like" data-article-id="'.$seq.'">' | |
.'<span class="heart-shape">♡</span> <span class="like-count">'.$row['like_count'].'</span></button></td>'; | |
?> | |
</tr> | |
<?php }?> | |
</tbody> | |
</table> | |
</form> | |
</div> | |
</div> | |
</div> | |
<?php include "modal.htm"; ?> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$host = 'a'; | |
$user = 'b'; | |
$pw = 'c'; | |
$dbName = 'd'; | |
$mysqli = new mysqli($host, $user, $pw, $dbName); | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
header('Content-Type: text/html; charset=utf-8'); | |
$writer = $_POST[writer]; | |
$message = $_POST[message]; | |
// echo $writer; | |
// echo $message; | |
include "initializeDB.php"; | |
if(!$mysqli){ | |
echo "MySQL 접속 실패: "; | |
} | |
$sql = "insert into messages values"; | |
$sql = $sql."(0, '$writer', '$message', default, 0)"; | |
if($mysqli -> query($sql)) { | |
echo "<script>location.href='index.php';</script>"; | |
} else { | |
echo "<script>"; | |
echo "alert('INSERT 오류발생');"; | |
echo "location.href='index.php';"; | |
echo "</script>"; | |
} | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
include 'initializeDB.php'; //$mysqli 변수 포함 | |
$ip = $ip = $_SERVER['REMOTE_ADDR']; // 사용자의 IP주소 가져오기 | |
$article_id = $_POST['articleId']; // 게시글 아이디 | |
$service_code = $_GET['getLikedByCode']; | |
// echo $service_code; | |
if(!empty($article_id)) { | |
$sql1 = "SELECT * from service_like WHERE service_code = 'phpex-$article_id' AND liked_ip = '$ip'"; | |
$res1 = mysqli_num_rows($mysqli->query($sql1)); // sql 의 행 갯수를 가져옴 | |
if($res1 == 0) { | |
// 좋아요 기록이 없는 경우 -> 좋아요 등록 | |
$sql2 = "INSERT into service_like VALUES(0, 'phpex-$article_id', '$ip', 1, sysdate())"; | |
$res2 = $mysqli->query($sql2); | |
// 게시판 테이블 업데이트 | |
$sql3 = "UPDATE messages SET like_count = like_count + 1 WHERE seq = $article_id"; | |
$res3 = $mysqli->query($sql3); | |
echo $res2 && $res3 ? "like" : "failed"; | |
} else { | |
// 이미 좋아요를 누른 경우 -> 좋아요 취소 | |
$sql2 = "DELETE from service_like WHERE service_code = 'phpex-$article_id' AND liked_ip = '$ip'"; | |
$res2 = $mysqli->query($sql2); | |
// 게시판 테이블 업데이트 | |
$sql3 = "UPDATE messages SET like_count = like_count - 1 WHERE seq = $article_id"; | |
$res3 = $mysqli->query($sql3); | |
echo $res2 && $res3 ? "unlike" : "failed"; | |
} | |
} else if(!empty($service_code)) { | |
$sql1 = "SELECT * from service_like WHERE service_code = 'phpex-$service_code' AND liked_ip = '$ip'"; | |
$res1 = mysqli_num_rows($mysqli->query($sql1)); // sql 의 행 갯수를 가져옴 | |
echo $res1 != 0 ? "liked" : "unliked"; | |
} | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div id=insertModal class="modal"> | |
<div class="modal-dialog" role="document"> | |
<div class="modal-content"> | |
<div class="modal-header"> | |
<h5 class="modal-title">INSERT</h5> | |
<!-- <button type="button" class="close" data-dismiss="modal" aria-label="Close"> | |
<span aria-hidden="true">×</span> | |
</button> --> | |
</div> | |
<div class="modal-body"> | |
<form id=insertFrm method=post action='insert_proc.php'> | |
<div class="form-group"> | |
<fieldset> | |
<label class="control-label" for="writerField">Writer</label> | |
<input class="form-control" id="writerField" name=writer type="text" placeholder="작성자 이름..."> | |
<hr> | |
<label class="control-label" for="messageField">Message</label> | |
<input class="form-control" id="messageField" name=message type="text" placeholder="내용..."> | |
</fieldset> | |
</div> | |
</form> | |
</div> | |
<div class="modal-footer"> | |
<button type="button" class="btn btn-primary" id=insertSubmitBtn>Send messages</button> | |
<button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button> | |
</div> | |
</div> | |
</div> | |
</div> | |
<div id=updateModal class="modal"> | |
<div class="modal-dialog" role="document"> | |
<div class="modal-content"> | |
<div class="modal-header"> | |
<h5 class="modal-title">UPDATE</h5> | |
<!-- <button type="button" class="close" data-dismiss="modal" aria-label="Close"> | |
<span aria-hidden="true">×</span> | |
</button> --> | |
</div> | |
<div class="modal-body"> | |
<form id=updateFrm method=post action='update_proc.php'> | |
<div class="form-group"> | |
<fieldset> | |
<label class="control-label" for="u_writerFieldU">Writer</label> | |
<input class="form-control" id="u_writerField" name=writer type="text" placeholder="작성자 이름..."> | |
<hr> | |
<label class="control-label" for="u_messageField">Message</label> | |
<input class="form-control" id="u_messageField" name=message type="text" placeholder="내용..."> | |
<input type=hidden id=u_seq name=seq> | |
</fieldset> | |
</div> | |
</form> | |
</div> | |
<div class="modal-footer"> | |
<button type="button" class="btn btn-primary" id=updateSubmitBtn>Update messages</button> | |
<button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button> | |
</div> | |
</div> | |
</div> | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(document).ready(function () { | |
function toggles(select) { | |
if (select == 1) { | |
$('input[name="seq"]').hide(); | |
$('span[id*="rownum"]').show(); | |
} else if (select == 0) { | |
$('input[name="seq"]').show(); | |
$('span[id*="rownum"]').hide(); | |
} else { | |
$('input[name="seq"]').toggle(); | |
$('span[id*="rownum"]').toggle(); | |
} | |
// 1 글자, 0 라디오 | |
} | |
toggles(1); | |
$('#insertBtn').click(function () { | |
$('#insertModal').modal() | |
}) | |
$('#insertSubmitBtn').click(function () { | |
if ($('#writerField').val() == '') { | |
alert('작성자를 입력해주세요.') | |
$('#writerField').focus() | |
} else if ($('#messageField').val() == '') { | |
alert('메시지를 입력해주세요.') | |
$('#messageField').focus() | |
} else { | |
$('#insertFrm').submit(); | |
} | |
}) | |
$('#selectBtn').click(toggles) | |
$('#deleteBtn').click(function () { | |
var checkedVal = $('input[name="seq"]:checked').val(); | |
if (typeof checkedVal != "undefined") { | |
$.ajax({ | |
url: "delete_proc.php", | |
type: "post", | |
data: $("#tableFrm").serialize(), | |
}).done(function (data) { | |
alert(data); | |
location.reload(); | |
}); | |
} else { | |
alert('삭제할 글을 선택해주세요.') | |
toggles(0); | |
} | |
}) | |
$('#updateBtn').click(function () { | |
var checkedVal = $('input[name="seq"]:checked').val(); | |
if (typeof checkedVal != "undefined") { | |
$('#u_writerField').val($('#writerTd' + checkedVal).text()) | |
$('#u_messageField').val($('#messageTd' + checkedVal).text()) | |
$('#u_seq').val(checkedVal) | |
$('#updateModal').modal() | |
} else { | |
alert('수정할 글을 선택해주세요.') | |
toggles(0); | |
} | |
}) | |
$('#updateSubmitBtn').click(function () { | |
if ($('#u_writerField').val() == '') { | |
alert('작성자를 입력해주세요.') | |
$('#u_writerField').focus() | |
} else if ($('#u_messageField').val() == '') { | |
alert('메시지를 입력해주세요.') | |
$('#u_messageField').focus() | |
} else { | |
$('#updateFrm').submit(); | |
} | |
}) | |
$(".btn-like").each(function(idx, el) { | |
var button = $(el) | |
var heartShape = button.find(".heart-shape") | |
$.get("./like_proc.php", { | |
getLikedByCode: button.data("articleId") | |
}, function(res) { | |
heartShape.text(res == "liked" ? "♥" : "♡") | |
button.fadeIn(500) | |
}) | |
}) | |
$(".btn-like").on("click", function(e) { | |
var button = $(e.currentTarget || e.target) | |
var likeCount = button.find(".like-count") | |
var heartShape = button.find(".heart-shape") | |
$.post("./like_proc.php", { | |
articleId: button.data("articleId") | |
}, function(res) { | |
console.log(res) | |
var addCount = (res == "like" ? 1 : res == "unlike" ? -1 : 0) | |
likeCount.text(+likeCount.text() + addCount) | |
heartShape.text(res == "like" ? "♥" : res == "unlike" ? "♡" : "♡") | |
}) | |
}) | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
header('Content-Type: text/html; charset=utf-8'); | |
$seq = $_POST[seq]; | |
$writer = $_POST[writer]; | |
$message = $_POST[message]; | |
include "initializeDB.php"; | |
if(!$mysqli){ | |
echo "MySQL 접속 실패: "; | |
} | |
$sql = "update messages set writer='$writer', message='$message'," | |
."wdate=sysdate() where seq='$seq'"; | |
if($mysqli -> query($sql)) { | |
echo "<script>location.href='index.php';</script>"; | |
} else { | |
echo "<script>"; | |
echo "alert('UPDATE 오류발생');"; | |
echo "location.href='index.php';"; | |
echo "</script>"; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment