Like Buttons
Einfacher include eines Like und dislike Buttons Erstinstallation:Tool kompletten Downloaden
Der hier verwendete Code
<?php
include('php_website/like-Buttons-main/likeserver.php');
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Like and Dislike system</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<style>
.posts-wrapper {
width: 50%;
margin: 20px auto;
border: 1px solid #eee;
}
.post {
width: 90%;
margin: 20px auto;
padding: 10px 5px 0px 5px;
border: 1px solid green;
}
.post-info {
margin: 10px auto 0px;
padding: 5px;
}
.fa {
font-size: 1.2em;
}
.fa-thumbs-down, .fa-thumbs-o-down {
transform:rotateY(180deg);
}
.logged_in_user {
padding: 10px 30px 0px;
}
i{
color: blue;
}
h2{
text-align:center;
}
main{
max-width:500px;
width:95%;
padding:15px;
overflow:hidden;
margin:0 auto;
border:1px solid black;
border-radius:10px;
box-shadow:5px 5px 5px black;
}
table{
width:100%;
border-collapse:collapse;
border:1px solid black;
}
td,th{
border:1px solid black;
}
table th{
background:lightgrey;
border-bottom:2px solid black;
}
form{
width:100%;
}
textarea{
width:100%;
margin:0 auto;
height:200px;
}
h1{
margin:0 auto;
width:100%;
text-align:center;
padding:10px 0;
text-decoration:underline;
}
.error{
color:red;
font-weight:900;
}
</style>
</head>
<body>
<div class="posts-wrapper">
<?php foreach ($posts as $post): ?>
<div class="post">
<?php echo $post['text']; ?>
<div class="post-info">
<i <?php if (userLiked($post['id'])): ?> class="fa fa-thumbs-up like-btn"<?php else: ?> class="fa fa-thumbs-o-up like-btn"<?php endif ?> data-id="<?php echo $post['id'] ?>"></i>
<span class="likes"><?php echo getLikes($post['id']); ?></span> Like
<i <?php if (userDisliked($post['id'])): ?> class="fa fa-thumbs-down dislike-btn" <?php else: ?> class="fa fa-thumbs-o-down dislike-btn" <?php endif ?> data-id="<?php echo $post['id'] ?>"></i>
<span class="dislikes"><?php echo getDislikes($post['id']); ?> </span> Dislike
</div>
</div>
<?php endforeach ?>
</div>
<h2>So sieht das Admin Menü aus</h2>
<?php
include('php_website/like-Buttons-main/mysql.php');
if (!$conn) {
die("Error connecting to database: " . mysqli_connect_error($conn));
exit();
}
$info='';
if(isset($_POST['kill'])){
$action_kill = mysqli_real_escape_string($conn, $_POST['kill']);
}
if(isset($_POST['new_like'])){
$action_new = mysqli_real_escape_string($conn, $_POST['new_like']);
}
if(isset($_POST['edit'])){
$action_edit = mysqli_real_escape_string($conn, $_POST['edit']);
}
if(isset($_POST['text'])){
$action_text = mysqli_real_escape_string($conn, $_POST['text']);
}
if (isset($action_new)) {
$dat=date("d.m.Y | H:i");
$query3="INSERT INTO `".$pref."posts` (`text`) VALUES ('$action_new')";
$result = mysqli_query($conn,$query3) or die ("MySQL-Error: " . mysqli_error($conn));
if($result){
$info.="Neuen Button erstellet <br>";
}else{
$info.="<div class='error'>Fehler beim erstellen von neuen Like Buttons <br>Diese Funktion ist bei der Demo deaktiviert</div>";
}
}
if (isset($action_kill)) {
$query3="DELETE FROM `".$pref."posts` WHERE `id`='$action_kill'";
// $result = mysqli_query($conn,$query3) or die ("MySQL-Error: " . mysqli_error($conn));
if($result1){
$info.="Like Button gklöscht <br>";
}else{
$info.="<div class='error'>Fehler beim löschen des Like Buttons <br>Diese Funktion ist bei der Demo deaktiviert</div>";
}
}
if (isset($action_edit) and isset($action_text)) {
$query3="UPDATE `".$pref."posts` SET `text`='$action_text' WHERE `id`='$action_edit'";
//$result = mysqli_query($conn,$query3) or die ("MySQL-Error: " . mysqli_error($conn));
if($result1){
$info.="Like Button bearbeitet <br>";
}else{
$info.="<div class='error'>Fehler beim bearbeiten</div>";
}
}
?>
<h1>Einstellungen Like Buttons</h1>
<main>
<?php
if (isset($action_edit) and !isset($action_text)) {
$query3="SELECT * FROM `".$pref."posts` WHERE `id`='$action_edit'";
$result = mysqli_query($conn,$query3) or die ("MySQL-Error: " . mysqli_error($conn));
if (mysqli_num_rows($result) == 1) {
$out=mysqli_fetch_array($result);
echo "<form method='POST'><input type='hidden' name='edit' value='$action_edit'><textarea name='text'>$out[1]</textarea><input type='submit' value='button Updaten'></form>";
}else{
echo "<div class='error'>Fehler beim bearbeiten</div>";
}
}else{
echo "<p>$info</p>";
echo "<table><tr><th>Id</th><th>Like Text</th><th></th><th></th></tr><tbody>";
$sql = "SELECT * FROM `".$pref."posts`";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
while($out=mysqli_fetch_array($result)){
echo "<tr>
<td>$out[0]</td>
<td>$out[1]</td>
<td><form method='post'><input type='hidden' name='kill' value='$out[0]'><button type='submit'><img src='php_website/like-Buttons-main/delete.png'></form></td>
<td><form method='post'><input type='hidden' name='edit' value='$out[0]'><button type='submit'><img src='php_website/like-Buttons-main/edit.png'></form></td>
</tr>";
}
}else{
echo "no Inhalt";
}
echo "</tbody></table>";
echo "<form method='POST'>
<label>Neuen Button erstellen</label><br>
<textarea maxlength='1000' name='new_like'></textarea>
<input type='submit' value='Neuen Button erstellen'>
</form>";
}
?>
<script>
$(document).ready(function(){
$('.like-btn').on('click', function(){
var post_id = $(this).data('id');
$clicked_btn = $(this);
if ($clicked_btn.hasClass('fa-thumbs-o-up')) {
action = 'like';
} else if($clicked_btn.hasClass('fa-thumbs-up')){
action = 'unlike';
}
$.ajax({
url: 'php_website/like-Buttons-main/likeserver.php',
type: 'post',
data: {
'action': action,
'post_id': post_id
},
success: function(data){
res = JSON.parse(data);
if (action == "like") {
$clicked_btn.removeClass('fa-thumbs-o-up');
$clicked_btn.addClass('fa-thumbs-up');
} else if(action == "unlike") {
$clicked_btn.removeClass('fa-thumbs-up');
$clicked_btn.addClass('fa-thumbs-o-up');
}
$clicked_btn.siblings('span.likes').text(res.likes);
$clicked_btn.siblings('span.dislikes').text(res.dislikes);
$clicked_btn.siblings('i.fa-thumbs-down').removeClass('fa-thumbs-down').addClass('fa-thumbs-o-down');
}
});
});
$('.dislike-btn').on('click', function(){
var post_id = $(this).data('id');
$clicked_btn = $(this);
if ($clicked_btn.hasClass('fa-thumbs-o-down')) {
action = 'dislike';
} else if($clicked_btn.hasClass('fa-thumbs-down')){
action = 'undislike';
}
$.ajax({
url: 'php_website/like-Buttons-main/likeserver.php',
type: 'post',
data: {
'action': action,
'post_id': post_id
},
success: function(data){
res = JSON.parse(data);
if (action == "dislike") {
$clicked_btn.removeClass('fa-thumbs-o-down');
$clicked_btn.addClass('fa-thumbs-down');
} else if(action == "undislike") {
$clicked_btn.removeClass('fa-thumbs-down');
$clicked_btn.addClass('fa-thumbs-o-down');
}
$clicked_btn.siblings('span.likes').text(res.likes);
$clicked_btn.siblings('span.dislikes').text(res.dislikes);
$clicked_btn.siblings('i.fa-thumbs-up').removeClass('fa-thumbs-up').addClass('fa-thumbs-o-up');
}
});
});
});
</script>
</body>
</html>