komentarze + rating dla nie/zalogowanych

This commit is contained in:
s416267 2018-12-16 20:05:52 +01:00
parent 1b1070786e
commit 31155fb925
4 changed files with 105 additions and 20 deletions

View File

@ -1,13 +1,13 @@
<?php <?php if( !isset( $_SESSION ) ) session_start();
include "../settings/db_connect.php"; include "../settings/db_connect.php";
$_SESSION['message'] = ''; $_SESSION['message'] = '';
$post=$_GET['post']; $post=$_GET['post'];
$user=$_SESSION['user'];
if ($_SERVER['REQUEST_METHOD'] == "POST"){ if ($_SERVER['REQUEST_METHOD'] == "POST"){
$tekst = $mysqli->real_escape_string($_POST['tekst']); $tekst = $mysqli->real_escape_string($_POST['tekst']);
$_SESSION['tekst'] = $tekst; $_SESSION['tekst'] = $tekst;
$sql = "INSERT INTO comments (tekst)" $sql = "INSERT INTO comments (post_id, username, tekst)"
. "Values ('$tekst')"; . "Values ('$post' , '$user' , '$tekst')";
if($mysqli->query($sql) === true){ if($mysqli->query($sql) === true){
$_SESSION['message'] = "Dodanie komentarza się powiodło!"; $_SESSION['message'] = "Dodanie komentarza się powiodło!";
header("location:post.php?post=$post"); header("location:post.php?post=$post");

View File

@ -29,6 +29,7 @@ z {
} }
post { post {
font-size: 18px; font-size: 18px;
@ -122,8 +123,9 @@ post {
line-height: 1.3; line-height: 1.3;
font-weight: 300; font-weight: 300;
text-align: left; text-align: left;
} }
.name-desc po .name-desc po
{ font-style: italic; { font-style: italic;
font-size: 19px; font-size: 19px;
@ -285,3 +287,4 @@ object-fit:scale-down;
.rate > label:hover ~ input:checked ~ label { .rate > label:hover ~ input:checked ~ label {
color: #f4c741; color: #f4c741;
} }

View File

@ -1,6 +1,8 @@
<?php if( !isset( $_SESSION ) ) session_start();
?>
<?php <?php
include "../settings/db_connect.php"; include "../settings/db_connect.php";
$post = $_GET['post']; $post = $_GET['post'];
if ($sql = $mysqli->prepare("SELECT blog_id FROM post WHERE post_id =$post")) if ($sql = $mysqli->prepare("SELECT blog_id FROM post WHERE post_id =$post"))
@ -15,6 +17,8 @@ if ($sql = $mysqli->prepare("SELECT blog_id FROM post WHERE post_id =$post"))
$sql->close(); $sql->close();
?> ?>
<html lang="pl-PL"> <html lang="pl-PL">
<head> <head>
<meta charset="utf-8"> <meta charset="utf-8">
@ -62,9 +66,23 @@ $sql->close();
<div class="row"> <div class="row">
<?php <?php
//wyliczanie średniej dla posta
if ($sql = $mysqli->prepare("SELECT ROUND(AVG(R1.rating),1) as averageRating FROM rating R1 RIGHT JOIN (SELECT MAX(R2.timestamp) AS timestamp FROM rating R2 GROUP BY R2.username) R2 ON R1.timestamp=R2.timestamp WHERE post_id=$post"))
{
$sql->execute();
$sql->bind_result($averageRating);
while ($sql->fetch()){}}
//ocena uzytkownika
if(!empty($_SESSION['user'])){
$user=$_SESSION['user'];
if ($sql = $mysqli->prepare("SELECT rating FROM rating WHERE post_id=$post AND username='$user'"))
{
$sql->execute();
$sql->bind_result($userRating);
while ($sql->fetch()){}} }
$trash = ''; $trash = '';
if ($sql = $mysqli->prepare("SELECT * FROM post WHERE post_id =$post")) if ($sql = $mysqli->prepare("SELECT * FROM post WHERE post_id =$post"))
{ {
@ -90,6 +108,31 @@ if ($sql = $mysqli->prepare("SELECT * FROM post WHERE post_id =$post"))
<br> <br>
</div> </div>
<?php if(empty($_SESSION['user'])){ ?>
<div class="container">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 name-desc">
<h4><?php echo $date; ?></h4>
</div><br>
<div class="col-lg-6 col-md-6 col-sm-6 col-xs-12 name-desc">
<div class="rate2">
<z>Średnia ocena: <?php echo $averageRating ?> </z>
</div>
</div>
</div>
<?php }
else{
?>
<div id="particles-js"> <div id="particles-js">
<div class="container"> <div class="container">
<form class="form" action="rating.php?post=<?php echo $post; ?>" method="post" enctype="multipart/form-data" autocomplete="off"> <form class="form" action="rating.php?post=<?php echo $post; ?>" method="post" enctype="multipart/form-data" autocomplete="off">
@ -97,11 +140,15 @@ if ($sql = $mysqli->prepare("SELECT * FROM post WHERE post_id =$post"))
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 name-desc"> <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 name-desc">
<h4><?php echo $date; ?></h4> <h4><?php echo $date; ?></h4>
</div><br> </div><br>
<div class="col-lg-6 col-md-6 col-sm-6 col-xs-12 name-desc elementsOnRegisterMain"> <div class="col-lg-6 col-md-6 col-sm-6 col-xs-12 name-desc elementsOnRegisterMain">
<div class="form-group inputmain"> <div class="form-group inputmain">
<div class="form-group"> <div class="form-group">
<div class="rate"> <div class="rate">
<z>Ocena:</z> <z>Twoja ocena: <?php echo $userRating ?> </z> <br>
<input type="submit" id="star5" name="rating" value=5 /> <input type="submit" id="star5" name="rating" value=5 />
<input type="submit" id="star4" name="rating" value=4 /> <input type="submit" id="star4" name="rating" value=4 />
@ -114,7 +161,9 @@ if ($sql = $mysqli->prepare("SELECT * FROM post WHERE post_id =$post"))
<label for="star3" title="3 gwiazdki">3 stars</label> <label for="star3" title="3 gwiazdki">3 stars</label>
<label for="star2" title="2 gwiazdki">2 stars</label> <label for="star2" title="2 gwiazdki">2 stars</label>
<label for="star1" title="1 gwiazdka">1 star</label> <label for="star1" title="1 gwiazdka">1 star</label>
</div>
</div><z>Średnia ocena: <?php echo $averageRating ?> </z>
</div> </div>
</div> </div>
@ -125,6 +174,14 @@ if ($sql = $mysqli->prepare("SELECT * FROM post WHERE post_id =$post"))
</div> </div>
</div> </div>
<?php ;} ?>
<?php } <?php }
$sql->close(); $sql->close();
} }
@ -132,15 +189,16 @@ else die( "Błąd w zapytaniu SQL! Sprawdź kod SQL w PhpMyAdmin." );
if ($sql = $mysqli->prepare("SELECT * FROM comments ORDER BY comment_id")) if ($sql = $mysqli->prepare("SELECT comments.comment_id, comments.post_id, comments.username, comments.tekst, comments.data_dodania, comments.data_modyfikacji, user.user_id FROM comments LEFT JOIN user ON comments.username=user.username WHERE post_id =$post ORDER BY comment_id"))
{ {
$sql->execute(); $sql->execute();
$sql->bind_result($comment_id,$post_id,$username,$tekst,$data_dodania, $data_modyfikacji); ?> $sql->bind_result($comment_id,$post_id,$username,$tekst,$data_dodania, $data_modyfikacji, $user_id); ?>
<div class="name-desc"> <h1> Komentarze</h1> </div> <div class="name-desc"> <h1>Komentarze</h1> </div>
<?php while ($sql->fetch()) <?php while ($sql->fetch())
{ ?> { ?>
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 name-desc"> <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 name-desc">
<a href="blog.php"><po><?php echo $username; ?></po></a> <a href="../blog-post/blog.php?user_id=<?php echo $user_id; ?>"><po><?php echo $username; ?></po></a>
<h6><?php echo $tekst; ?></h6> <h6><?php echo $tekst; ?></h6>
<h5><?php echo $data_modyfikacji; ?></h5> <h5><?php echo $data_modyfikacji; ?></h5>
</div> </div>
@ -148,8 +206,30 @@ if ($sql = $mysqli->prepare("SELECT * FROM comments ORDER BY comment_id"))
$sql->close(); $sql->close();
$mysqli->close(); $mysqli->close();
} ?> } ?>
<div class="name"> <h2> Dodaj komentarz:</h2> </div> <div class="name"> <h2> Dodaj komentarz:</h2> </div>
<?php
if(empty($_SESSION['user'])){ ?>
<div class="container">
<form class="form" action="../admin-zone/login.php?post=<?php echo $post; ?>" method="post" enctype="multipart/form-data" autocomplete="off">
<div class="row registerFooter">
<div class="offset-3 col-6 offset-3 elementsOnRegisterFooter">
<div>
<input type="submit" value="Zaloguj się by komentować i oceniać wpisy" name="register" class="btn btn-block" />
</div>
</div>
</div>
</form>
</div>
<?php
}
else{
?>
<div class="container"> <div class="container">
<form class="form" action="comment.php?post=<?php echo $post; ?>" method="post" enctype="multipart/form-data" autocomplete="off"> <form class="form" action="comment.php?post=<?php echo $post; ?>" method="post" enctype="multipart/form-data" autocomplete="off">
<div class="row registerMain"> <div class="row registerMain">
@ -170,5 +250,6 @@ if ($sql = $mysqli->prepare("SELECT * FROM comments ORDER BY comment_id"))
</div> </div>
</form> </form>
</div> </div>
<?php ;} ?>
</body> </body>
</html> </html>

View File

@ -1,12 +1,13 @@
<?php <?php if( !isset( $_SESSION ) ) session_start();
include "../settings/db_connect.php"; include "../settings/db_connect.php";
$_SESSION['message'] = ''; $_SESSION['message'] = '';
$post=$_GET['post']; $post=$_GET['post'];
$user=$_SESSION['user'];
if ($_SERVER['REQUEST_METHOD'] == "POST"){ if ($_SERVER['REQUEST_METHOD'] == "POST"){
$rating = $mysqli->real_escape_string($_POST['rating']); $rating = $mysqli->real_escape_string($_POST['rating']);
$_SESSION['rating'] = $rating; $_SESSION['rating'] = $rating;
$sql = "INSERT INTO rating (rating)" $sql = "INSERT INTO rating (post_id, username, rating)"
. "Values ($rating)"; . "Values ($post, '$user', $rating)";
if($mysqli->query($sql) === true){ if($mysqli->query($sql) === true){
$_SESSION['message'] = "Dodanie oceny się powiodło!"; $_SESSION['message'] = "Dodanie oceny się powiodło!";