Зона комментариев

Тема в разделе "PHP для новичков", создана пользователем Oogily, 13 мар 2020.

  1. Oogily

    Oogily Активный пользователь

    С нами с:
    25 авг 2009
    Сообщения:
    33
    Симпатии:
    1
    Добрый день! Есть страница, на которой нужно вывести комментарии. Выводит так:
    (картинка 1 во вложении)
    А хотелось бы так:
    (картинка 2 во вложении)
    В общем, задача заключается в сворачивании "ответов" в один спойлер (не каждый в своём, а все в одном),
    ещё, если не затруднит, можете посоветовать другой спойлер, не <details>
    Код:
    (!!! много букв !!!)
    PHP:
    <!-- PHP SETTINGS BEGIN -->


    <?php
    $time
    =time();
    if (
    session_id()=='') session_start();

    $db=mysqli_connect("localhost","id12877352_qwerty","qwerty","id12877352_commets") or die();
    $res=mysqli_query($db,"set names utf8");

    $mess_url='https://3forum.000webhostapp.com/index.php';

    //получаем id текущей темы
    $res=mysqli_query($db,"SELECT id FROM таблица WHERE file_name='".$mess_url."'");
    $res=mysqli_fetch_array($res);
    $theme_id=0;

    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, theme_id, login, message)
        values ('"
    .$_POST["parent_id"]."','".$_POST["f_parent"]."',
        '"
    .$time."','".$theme_id."','".$mess_login."','".$user_text."')");
       else 
    $res=mysqli_query($db,"insert into comment (date, theme_id, login, message)
       values ('"
    .$time."','".$theme_id."','".$mess_login."','".$user_text."')");
        
    $_SESSION["send"]="Комментарий принят!";
        
    header("Location: index.php#last"); #exit;
      
    }
      else {
       
    $_SESSION["send"]="Не все поля заполнены!";
       
    header("Location: index.php#last"); #exit;
      
    }
    }
    else {
      
    $_SESSION["send"]="Неверный проверочный код!";
      
    header("Location: index.php#last"); #exit;
    }
    }

    if (isset(
    $_SESSION["send"]) and $_SESSION["send"]!="") {    //вывод сообщения
        
    echo '<script type="text/javascript">alert("'.$_SESSION["send"].'");</script>';
        
    $_SESSION["send"]="";
    }
    ?>


    <!-- PHP SETTINGS END -->


    <!-- HTML BEGIN -->


    <html lang="ru">
    <head>
        <meta charset="UTF-8">
        <link rel="icon" href="images/favicon.ico">
        <title>3 форум - </title>
        <style type="text/css">
            .header {
                height: 120px;
                background-color: green;
                margin: 5px;
                border: 2px solid #DFD;
            }
            .content {
                background-color: green;
                margin: 5px;
            }
            .footer {
                background-color: green;
                margin: 5px;
            }
            .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('images/minus.png') no-repeat; }
                .comm_plus { background: url('images/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(images/strelka.png) no-repeat;
                border-left: 2px solid #000;
            }
            .strelka_2 { background: url(images/strelka_2.png) no-repeat; }
            #hint { position: absolute; display: none; z-index: 100; }
        </style>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
        <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>
    </head>
    <body background="images/bg.jpg">
        <div class="header">
            <h1>header</h1>
        </div>
        <div class="content">
            <h1>content</h1>
         
         
            <!-- COMMENTS AREA BEGIN -->
         
            <!-- COMMENTS LIST AREA BEGIN -->
    <?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">';
    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 theme_id='"
    .$theme_id."' ORDER BY id"); #and moderation=1
    $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>';
    }
    ?>
         
            <!-- COMMENTS LIST AREA END -->
         
    <?php
    $cod
    =rand(1,5); $cod2=rand(5,10);
    echo 
    '<div id="last" align="center">';
    echo 
    '<form method="POST" action="'.$mess_url.'#last" 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">&nbsp;';
    echo 
    '<input type="submit" value="Отправить"></div>';
    echo 
    '</form>';

    echo 
    '<form method="POST" action="'.$mess_url.'#last" 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">&nbsp;';
    echo 
    '<input type="submit" value="Отправить"></div>';
    echo 
    '</form></div>';
    ?>
         
            <!-- COMMENTS AREA END -->
         
        </div>
        <div class="footer">
            <h1>footer</h1>
        </div>
    </body>
    </html>


    <!-- HTML END -->
    Простите за немного зеленоватый оттенок и краткость содержания, страница в разработке. Заранее очень благодарен за подсказанное решение.
    картинка 1.PNG картинка 2.PNG
    Если будут нужны какие-либо другие элементы страницы, пишите, отправлю!