Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/232.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为数据库中的每一行创建一个新的div(及其内容)_Javascript_Php_Html_Css - Fatal编程技术网

Javascript 为数据库中的每一行创建一个新的div(及其内容)

Javascript 为数据库中的每一行创建一个新的div(及其内容),javascript,php,html,css,Javascript,Php,Html,Css,我有一个功能,用户可以发布想法,用户可以为每个想法添加评论。它的工作方式是,当点击评论链接时,所有与该想法id相关的评论都将被加载 以下是我的用户注释表的结构: 考虑以下数据: 用户想法表1行: id: 184 thought: "hello, this is a thought from anderson." 假设用户注释表中有两行: 问题:目前,当我点击评论链接时,只有一条评论被显示,最新的评论被显示,所以在本例中,爱丽丝的评论 代码如下: 我的想法是: 这可能只是我在努力寻找解决方案

我有一个功能,用户可以发布想法,用户可以为每个想法添加评论。它的工作方式是,当点击评论链接时,所有与该想法id相关的评论都将被加载

以下是我的用户注释表的结构:

考虑以下数据:

用户想法表1行:

 id: 184
 thought: "hello, this is a thought from anderson."
假设用户注释表中有两行:

问题:目前,当我点击评论链接时,只有一条评论被显示,最新的评论被显示,所以在本例中,爱丽丝的评论

代码如下:

我的想法是: 这可能只是我在努力寻找解决方案,但是,是否每个评论都相互重叠?我的评论功能涉及到在思想下面动态出现的评论,所以我使用了Javascript来实现这一点。只是想用一条新的评论来取代这个区块

我所尝试的:

当有两条注释要显示时,仍然只显示一条注释


为什么不使用ajax呢?我曾经做过一个使用stackoverflow这样的评论的网站,为此我使用了很多ajax。当然,所有html元素的创建都将在js中完成,您将从php返回的只是包含注释内容及其信息的json。 这将帮助您加载新注释,而无需刷新页面设置间隔

对于答案,我发现了三件奇怪的事情,第一件是

if ($num_of_comments !=0 || $num_of_comments == 0){ 

这永远是真的。第二件事是,回音在集团之外,而这可能是一条评论被回音的原因。最后一件事是在html元素的样式中不显示任何内容。因此,我建议您在while块中创建echo,或者创建一个注释数组并创建一个迭代器。之后,尝试使用浏览器的inspector工具查看返回的代码源是否只包含一条或多条注释。这将帮助您查看php是否有效

您需要在while循环中回显注释。现在,您正在每次迭代中设置变量,但在退出循环后,只有最后一个变量在div中得到响应。@AdamKonieska-我已经尝试过了,但它仍然只显示一行。我已经编辑了我的问题,以显示echo的div的确切位置。您正在设置display:none;在他们身上。你确定他们没有被回应吗?$num_of_comments的值是多少?@AdamKonieska-$num_of_comments返回值2,这是真的,因为它们在表中有两行。但只显示一行。我将编辑我的答案以提供一个图像,也许它可以更好地说明问题。Freddy,图像没有帮助,因为它没有显示正在呈现的完整标记。检查源代码,并在此处发布呈现的HTML输出。根据到目前为止的情况,这两条评论似乎都应该得到回应,但显示为:无;可能正在对用户隐藏它们。
   id: 1
   body_of_msg: Comment assigned to thought_id of 184
   comment_posted_by: conor
   comment_posted_to: anderson
   post_id: 184

   id: 2
   body_of_msg: Another comment assigned to thought_id of 184
   comment_posted_by: alice
   comment_posted_to: anderson
   post_id: 184
<?php
    // Get the comments attached to a users post...
    $get_comm = mysqli_query ($connect, "SELECT * FROM user_comments WHERE post_id='$thought_id' ORDER BY post_id DESC");
    $num_of_comments = mysqli_num_rows($get_comm); // get number of comments for each post by post_id
    // if there are comments for the post, get its content
    if ($num_of_comments !=0 || $num_of_comments == 0){ 
    while(  $comment = mysqli_fetch_assoc ($get_comm)){
            $comment_body           = $comment['body_of_msg'];
            $comment_posted_to      = $comment['comment_posted_to'];
            $comment_posted_by      = $comment['comment_posted_by'];
            $removed                = $comment['comment_removed'];
    }
    echo   "";          
            /** There are other divs and content echo'd here**/
           ////////////////////////////////////////////
            // this is where each comment is displayed
            echo "
            <div id='toggleComment$thought_id' class='new_comment' style='display:none;'>
                <br/><b><a href = 'profile_page/$comment_posted_by'> $comment_posted_by said</a></b>: $comment_body "; ?><?php
                    if ($comment_posted_by == $username){
                            echo "<a id='remove_comment' href = '/inc/remove_comment.php'> Delete </a>";                        
                        } echo "
            </div>";
            /////////////////////////////////////////////   
    }
?>
$count = mysqli_query ($connect, "SELECT * FROM user_thoughts");
while ($row = mysqli_fetch_assoc($get_thoughts_from_db)) {
    $thought_id      = $row['id'];
}
while(  $comment = mysqli_fetch_assoc ($get_comm)){
        $comment_body           = $comment['body_of_msg'];
        $comment_posted_to      = $comment['comment_posted_to'];
        $comment_posted_by      = $comment['comment_posted_by'];
        $removed                = $comment['comment_removed'];
        // this is where each comment is displayed
        echo "
        <div id='toggleComment$thought_id' class='new_comment' style='display:none;'>
            <br/><b><a href = 'profile_page/$comment_posted_by'> $comment_posted_by said</a></b>: $comment_body "; ?><?php
                if ($comment_posted_by == $username){
                        echo "<a id='remove_comment' href = '/inc/remove_comment.php'> Delete </a>";                        
                    } echo "
        </div>";
        } 
    }
if ($num_of_comments !=0 || $num_of_comments == 0){