<!DOCTYPE html>
<html>
<script type="text/javascript"
src="//ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<?php
$time=time();
if (session_id()=='') session_start();
$db=mysqli_connect('...') or die();
$res=mysqli_query($db,"set names utf8");
$mess_url=mysqli_real_escape_string($db, basename($_SERVER['SCRIPT_FILENAME']));
//получаем id текущей темы
$id=$_GET['id'];
$res=mysqli_query($db,"SELECT id FROM movies WHERE id=$id");
$res=mysqli_fetch_array($res);
$movies_id=$res["id"];
if (isset($_POST["contr_cod"])){ //отправлен комментарий
$mess_login=htmlspecialchars($_POST["mess_login"]);
$user_text=htmlspecialchars($_POST["user_text"]);
if (md5($_POST["contr_cod"])==$_POST["prov_summa"]){ //код правильный
if ($mess_login!='' and $user_text!=''){
if (is_numeric($_POST["parent_id"]) and is_numeric($_POST["f_parent"]))
$res=mysqli_query($db,"insert into comment
(parent_id, first_parent, date, movies_id, login, message)
values ('".$_POST["parent_id"]."','".$_POST["f_parent"]."',
'".$time."','".$movies_id."','".$mess_login."','".$user_text."')");
else $res=mysqli_query($db,"insert into comment (date, movies_id, login, message)
values ('".$time."','".$movies_id."','".$mess_login."','".$user_text."')");
$_SESSION["send"]="Комментарий принят!";
header("Location: $mess_url#last"); exit;
}
else {
$_SESSION["send"]="Не все поля заполнены!";
header("Location: $mess_url#last"); exit;
}
}
else {
$_SESSION["send"]="Неверный проверочный код!";
header("Location: $mess_url#last"); exit;
}
}
if (isset($_SESSION["send"]) and $_SESSION["send"]!="") { //вывод сообщения
echo '<script type="text/javascript">alert("'.$_SESSION["send"].'");</script>';
$_SESSION["send"]="";
}
?>
<head>
<title>Hello!</title>
</head>
<style type="text/css">
.add_comment {
display: table;
width: 580px;
border: 1px solid #000;
background-color: #6AF;
}
.close_hint, .open_hint {
float: right;
border: 1px solid #77A;
background: #6e6;
width: 100px;
text-align: center;
cursor: pointer;
}
.close_hint { margin: 5px; color: #F00; }
.comm_body { padding: 0 5px; background-color:#EEE; text-align:left; }
.comm_head { padding: 3px; border: 1px solid #77A; background-color: #DFD; }
.comm_minus { background: url('image/minus.png') no-repeat; }
.comm_plus { background: url('image/plus.png') no-repeat; }
.comm_minus, .comm_plus {
float: right;
width: 19px;
height: 18px;
cursor: pointer;
}
.comm_text { display:none; }
.sp_link { color: #F33; cursor: pointer; }
.strelka {
background: url(image/strelka.png) no-repeat;
border-left: 2px solid #000;
}
.strelka_2 { background: url(image/strelka_2.png) no-repeat; }
#hint { position: absolute; display: none; z-index: 100; }
</style>
<body>
<script type="text/javascript">
//Добавление в форму отправки комментария значений id родительских комментариев
function comm_on(p_id,first_p){
document.add_comment.parent_id.value=p_id;
document.add_comment.f_parent.value=first_p;
}
$(document).ready(function(){
//Показать скрытое под спойлером сообщение
$(".sp_link").click(function(){
$(this).parent().children(".comm_text").toggle("normal");
});
//Показать форму ответа на имеющийся комментарий
$(".open_hint").click(function(){
$("#hint").animate({
top: $(this).offset().top + 25, left: $(document).width()/2 -
$("#hint").width()/2
}, 400).fadeIn(800);
});
//Скрыть форму ответа на имеющийся комментарий
$(".close_hint").click(function(){ $("#hint").fadeOut(1200); });
//Получение id оцененного комментария
$(".comm_plus,.comm_minus").click(function(){
id_comm=$(this).parents(".comm_head").attr("id").substr(1);
});
//Отправление оценки комментария в файл rating_comm.php
$(".comm_plus").click(function(){
jQuery.post("rating_comm.php",{comm_id:id_comm,ocenka:1},rating_comm);
});
$(".comm_minus").click(function(){
jQuery.post("rating_comm.php",{comm_id:id_comm,ocenka:0},rating_comm);
});
//Возврат рейтинга комментария и его обновление
function rating_comm(data){
$("#rating_comm"+id_comm).fadeOut(800,function(){
$(this).html(data).fadeIn(800);
});
}
});
</script>
<dl>
<?php
// Соединиться с сервером БД
$db = mysqli_connect('***', '***', '***', '***') or die (mysqli_error ());
// Получить данные из БД, в зависимости от значения id в URL
$rs = mysqli_query($db, "set names cp1251");
$rs = mysqli_query($db, "SELECT * FROM movies WHERE id=$id");
// Цикл по $rs
while($row = mysqli_fetch_array($rs)) {
// Записать данные человека
echo "<dt>Название:</dt><dd>" . $row['name'] . "</dd>";
echo "<dt>Год:</dt><dd>" . $row['year'] . "</dd>";
echo "<dt>Оригинальное название:</dt><dd>" . $row['eng_name'] . "</dd>";
}
?>
</dl>
<?php
function parents($up=0, $left=0) { //Строим иерархическое дерево комментариев
global $tag,$mess_url;
for ($i=0; $i<=count($tag[$up])-1; $i++) {
//Можно выделять цветом указанные логины
if ($tag[$up][$i][2]=='Admin') $tag[$up][$i][2]='<font color="#C00">Admin</font>';
if ($tag[$up][$i][6]==0) $tag[$up][$i][6]=$tag[$up][$i][0];
//Высчитываем рейтинг комментария
$sum=$tag[$up][$i][4]-$tag[$up][$i][5];
if ($up==0) echo '<div style="padding:5px 0 0 0;">';
else {
if (count($tag[$up])-1!=$i)
echo '<div class="strelka" style="padding:5px 0 0 '.($left-2).'px;">';
else echo '<div class="strelka_2" style="padding:5px 0 0 '.$left.'px;">';
}
echo '<div class="comm_head" id="m'.$tag[$up][$i][0].'">';
echo '<div style="float:left;"><b>'.$tag[$up][$i][2].'</b></div>';
echo '<div class="comm_minus"></div>';
echo '<div style="float:right; width:30px;" id="rating_comm'.$tag[$up][$i][0].'">';
echo '<b>'.$sum.'</b></div><div class="comm_plus"></div>';
echo '<a style="float:right; width:70px;" href="'.$mess_url.'#m';
echo $tag[$up][$i][0].'"># '.$tag[$up][$i][0].'</a>';
echo '<div style="float:right; width:170px;">';
echo '('.date("H:i:s d.m.Y", $tag[$up][$i][3]).' г.)</div>';
echo '<div style="clear:both;"></div></div>';
echo '<div class="comm_body">';
if ($sum<0) echo '<u class="sp_link">Показать/скрыть</u><div class="comm_text">';
else echo '<div style="word-wrap:break-word;">';
echo str_replace("<br />","<br>",nl2br($tag[$up][$i][1])).'</div>';
echo '<div class="open_hint" onClick="comm_on('.$tag[$up][$i][0].',
'.$tag[$up][$i][6].')">Комментировать</div><div style="clear:both;"></div></div>';
if (isset($tag[ $tag[$up][$i][0] ])) parents($tag[$up][$i][0],20);
echo '</div>';
}
}
$res=mysqli_query($db,"SELECT * FROM comment
WHERE movies_id='".$movies_id."' ORDER BY id");
$number=mysqli_num_rows($res);
if ($number>0) {
echo '<div style="border:1px solid #000000;padding:5px;text-align:center;">';
echo '<b>Последние комментарии:</b><br>';
while ($com=mysqli_fetch_assoc($res))
$tag[(int)$com["parent_id"]][] = array((int)$com["id"], $com["message"],
$com["login"], $com["date"], $com["plus"], $com["minus"], $com["first_parent"]);
echo parents().'</div><br>';
}
$cod=rand(100,900); $cod2=rand(1,99);
echo '<div id="last" align="center">';
echo "<form method='POST' action='movie.php?id=".$row['id']."' class='add_comment'";
echo 'name="add_comment" id="hint"><div class="close_hint">Закрыть</div>';
echo '<textarea cols="68" rows="5" name="user_text"></textarea>';
echo '<div style="margin:5px; float:left;">';
echo 'Имя: <input type="text" name="mess_login" maxlength="20" value=""></div>';
echo '<div style="margin:5px; float:right;">'.$cod.' + '.$cod2.' = ';
echo '<input type="hidden" name="prov_summa" value="'.md5($cod+$cod2).'">';
echo '<input type="hidden" name="parent_id" value="0">';
echo '<input type="hidden" name="f_parent" value="0">';
echo '<input type="text" name="contr_cod" maxlength="4" size="4"> ';
echo '<input type="submit" value="Отправить"></div>';
echo '</form>';
echo "<form method='POST' action='movie.php?id=".$row['id']."' class='add_comment'>";
echo 'Написать комментарий:';
echo '<textarea cols="68" rows="5" name="user_text"></textarea>';
echo '<div style="margin:5px; float:left;">';
echo 'Имя: <input type="text" name="mess_login" maxlength="20" value=""></div>';
echo '<div style="margin:5px; float:right;">'.$cod.' + '.$cod2.' = ';
echo '<input type="hidden" name="prov_summa" value="'.md5($cod+$cod2).'">';
echo '<input type="text" name="contr_cod" maxlength="4" size="4"> ';
echo '<input type="submit" value="Отправить"></div>';
echo '</form></div>';
?>
<p><a href="list.php">Вернутся к списку</a></p>
</body>
</html>