php - 将 2 级回复转换为无限回复

标签 php html mysql css nested

我正在尝试将我的回复从 2 级转换为无限回复。目前如果只显示回复的回复,不显示回复的回复等等。嵌套。

代码:

<? 
include 'connect.php'; 
$error = $_GET['er'];
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Group 5 Waggle</title>

    <link rel="stylesheet" type="text/css" href="bootstrap/css/bootstrap.min.css" />
    <link rel="stylesheet" type="text/css" href="font-awesome/css/font-awesome.min.css" />
    <link rel="stylesheet" type="text/css" href="css/local.css" />

    <script type="text/javascript" src="js/jquery-1.10.2.min.js"></script>
    <script type="text/javascript" src="bootstrap/js/bootstrap.min.js"></script>   
</head>
<body>

    <div id="wrapper">
    <? include 'nav.tmp'; ?>

<?
$sql = "SELECT
            topic_id,
            topic_subject,
            topic_cat
        FROM
            topics
        WHERE
            topics.topic_id = " . mysql_real_escape_string($_GET['id']);

$result = mysql_query($sql);

if(!$result)
{
    echo '<div class="well well-lg">The topic could not be displayed, please try again later.</div>';
}
else
{
    if(mysql_num_rows($result) == 0)
    {
        echo '<div class="well well-lg">This topic doesn&prime;t exist.</div>';
    }
    else
    {
        while($row = mysql_fetch_assoc($result))
        { ?>
                    <div class="jumbotron">
                    <h1><?=$row['topic_subject'];?></h1>
                    <a href="reply.php?id=<?=$row['topic_id'];?>" class="btn btn-primary btn-sm">Reply To Post</a>
                    <!--<a href="topics.php?id=<?=$row['topic_cat'];?>">Library</a>-->
                    </div>  


        <div id="page-wrapper">

            <div class="row">

                <div class="col-lg-12">

                    <div class="bs-example">
                    <div class="list-group">

                    <? if ($error == 'pr') { ?>
                     <div class="alert alert-dismissable alert-danger">
                        <button type="button" class="close" data-dismiss="alert">×</button>
                        <strong>Oh snap!</strong> Couldn't delete that post. It's a parent post with a child. Child has to be deleted first.
                    </div>
                    <? } ?>

                    <!--
                    <div class="jumbotron">
                    <h1>Post One</h1>
                    <div class="small"><span style="float: left"><a href="topics.html"><i class="fa fa-user"></i> Olajide</a></span>&nbsp;<span style="float: right">July 16, 2015 <i class="fa fa-share-alt"></i></span></div>
                    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam nec enim sit amet libero ultrices faucibus quis id purus. Nam feugiat libero eget felis semper hendrerit in et nunc.</p>
                    <button type="button" class="btn btn-primary btn-sm">Reply To Post</button>
                    </div>  


                    <div class="panel panel-default">
                        <div class="panel-heading"><a href="topics.html"><span style="float: left"><i class="fa fa-user"></i> Nick</span></a>&nbsp;<span style="float: right"><i class="fa fa-share-alt"></i></span></div>
                        <div class="panel-body">
                            PLorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam libero leo, bibendum id consectetur quis, pretium in metus. Morbi non tempus tortor. Sed libero est, euismod et velit eu, posuere maximus sem. Sed tincidunt finibus condimentum. Nam a diam a neque tincidunt egestas at non massa. Proin consequat non est quis tristique. Fusce aliquet lacus sed urna tincidunt convallis. Phasellus mattis lectus sed nunc ornare finibus. Duis nec orci vitae risus vestibulum pulvinar a sit amet tortor. Proin in lacinia odio. Etiam cursus ac elit sed scelerisque. Sed egestas est sit amet venenatis facilisis. Etiam at fermentum eros, et sollicitudin libero.

                            Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Sed neque risus, elementum et fermentum sit amet, euismod a lectus. Ut hendrerit vulputate arcu, id bibendum nibh venenatis ut. Nam eu quam lorem. Morbi eget varius velit. Nulla dictum metus sit amet lorem posuere, at sagittis tortor facilisis. Nunc auctor, est at gravida ultricies, dolor libero commodo nulla, eu aliquam nulla massa vitae odio. Sed pellentesque eleifend dui, consectetur maximus neque lacinia nec. Suspendisse quis quam ultrices, aliquet nulla quis, sagittis orci. Vestibulum id ligula nec lorem rutrum efficitur. Donec ac porta felis, sed aliquam justo. Pellentesque gravida massa sem, non suscipit mi consectetur et. Etiam posuere pharetra ante eget cursus.

                            Vivamus gravida eget metus sed facilisis. Praesent eu sem orci. Interdum et malesuada fames ac ante ipsum primis in faucibus. Duis ullamcorper eros massa, at tincidunt dui volutpat non. Duis gravida, velit vitae consectetur ultrices, mi metus aliquam nisl, sed mattis ligula mauris commodo enim. Cras magna risus, tristique eu orci rutrum, porta egestas felis. Curabitur dignissim nunc sed ipsum commodo maximus.
                            <br />
                            <br />
                            <button type="button" class="btn btn-primary btn-sm">Reply To Post</button>
                        </div>
                        <div class="panel-footer"><span style="float: left" class="small">Posted On: July 18, 2015</span>&nbsp;<span style="float: right">&nbsp;</span></div>
                    </div>
                    -->

                    <ul id="comments">
<?        
            //fetch the posts from the database
            $posts1_sql = "SELECT
                        posts.post_topic,
                        posts.post_content,
                        posts.post_id,
                        posts.post_date,
                        posts.post_by,
                        posts.post_parent,
                        posts.file_uploaded,
                        users.user_id,
                        users.user_name,
                        users.user_pic
                    FROM
                        posts
                    LEFT JOIN
                        users
                    ON
                        posts.post_by = users.user_id
                    WHERE
                        posts.post_parent = 0 AND posts.post_topic = " . mysql_real_escape_string($_GET['id']);

            $posts1_result = mysql_query($posts1_sql);

            if(!$posts1_result)
            {
                echo '<div class="well well-lg">The posts could not be displayed, please try again later.</div>';
            }
            else
            {

                while($posts1_row = mysql_fetch_assoc($posts1_result))
                { 

                    ?>

                    <li class="cmmnt">
                    <div class="avatar"><a href="profile.php?id=<?=$posts1_row['user_id'];?>"><img src="profilepics/<?=$posts1_row['user_pic'];?>" width="55" height="55" alt="Avatar"></a></div>
                    <div class="cmmnt-content">
                    <header><a href="profile.php?id=<?=$posts1_row['user_id'];?>" class="userlink"><span class="pubname"><?=$posts1_row['user_name'];?></span></a> <span class="pubdate text-uppercase"><?=date('F d, Y H:i', strtotime($posts1_row['post_date']));?></span></header>
                    <p><?=htmlentities(stripslashes($posts1_row['post_content']));?></p>
                    <?
                    $fileName = htmlentities(stripslashes($posts1_row['file_uploaded']));
                    if($fileName !=NULL)
                        { ?> 
                            <p>
                            <i class="fa fa-paperclip"></i> <a href="repository/<?=$fileName?>" download="<?=$fileName?>"><?=$fileName?></a> 
                            </p>
                        <?
                        }?>

                    <a class="btn btn-primary btn-xs text-uppercase" href="reply.php?id=<?=$row['topic_id'];?>&r=<?=$posts1_row['post_id'];?>" id="reply"><span class="glyphicon glyphicon-share-alt"></span> Reply</a>
                    <? if($_SESSION['signed_in'] == true) { ?>
                    <? if(($_SESSION['user_id'] == $posts1_row['post_by']) | ($_SESSION['user_level'] == 0)) { ?><a href="edit.php?id=<?=$posts1_row['post_id'];?>"><i class="fa fa-pencil-square-o"></i></a><? }?> <? if ($_SESSION['user_level'] == 0) { ?> <a href="delete.php?id=<?=$posts1_row['post_id'];?>"><i class="fa fa-times-circle-o"></i></a><? } ?>
                    <? } ?>
                    </div>

                        <?
                        //fetch the posts from the database
                        $posts1id = mysql_real_escape_string($posts1_row['post_id']);
                        $postid = mysql_real_escape_string($_GET['id']);

                        $posts2_sql = "SELECT posts.post_topic, posts.post_content, posts.post_id, posts.post_date, posts.file_uploaded, posts.post_by, posts.post_parent, users.user_id, users.user_name, users.user_pic FROM posts LEFT JOIN users ON posts.post_by = users.user_id WHERE posts.post_parent = '$posts1id'  AND posts.post_topic = '$postid'";

                        $posts2_result = mysql_query($posts2_sql);

                        if(!$posts2_result)
                        {
                            echo '<div class="well well-lg">The posts could not be displayed, please try again later.</div>';
                            //echo '<script>$(document).ready(function(){ $("#ee'.$posts1id.'").hide(); });</script>';
                        }
                        else if(mysql_num_rows($posts2_result) == 0) 
                        { 
                            //echo '<script>$(document).ready(function(){ $("#ee'.$posts1id.'").hide(); });</script>';
                        } 
                        else
                        {

                            while($posts2_row = mysql_fetch_assoc($posts2_result))
                            { 

                                ?>


                                <ul class="replies">
                                <li class="cmmnt">
                                    <div class="avatar"><a href="profile.php?id=<?=$posts2_row['user_id'];?>"><img src="profilepics/<?=$posts2_row['user_pic'];?>" width="55" height="55" alt="Avatar"></a></div>
                                    <div class="cmmnt-content">
                                    <header><a href="profile.php?id=<?=$posts2_row['user_id'];?>" class="userlink"><span class="pubname"><?=$posts2_row['user_name'];?></span></a> <span class="pubdate text-uppercase"><?=date('F d, Y H:i', strtotime($posts2_row['post_date']));?></span></header>
                                    <p><?=htmlentities(stripslashes($posts2_row['post_content']));?></p>
                                    <?
                                    $replyFileName = htmlentities(stripslashes($posts2_row['file_uploaded']));
                                    if($replyFileName !=NULL)
                                        { ?> 
                                            <p>
                                            <i class="fa fa-paperclip"></i> <a href="repository/<?=$replyFileName?>" download="<?=$replyFileName?>"><?=$replyFileName?></a>
                                            </p>
                                        <?
                                        }?>

                                    <a class="btn btn-primary btn-xs text-uppercase" href="reply.php?id=<?=$row['topic_id'];?>&r=<?=$posts2_row['post_id'];?>" id="reply"><span class="glyphicon glyphicon-share-alt"></span> Reply</a>                                   
                                    <? if($_SESSION['signed_in'] == true) { ?>
                                    <? if(($_SESSION['user_id'] == $posts2_row['post_by']) | ($_SESSION['user_level'] == 0)) { ?><a href="edit.php?id=<?=$posts2_row['post_id'];?>"><i class="fa fa-pencil-square-o"></i></a><? }?> <? if ($_SESSION['user_level'] == 0) { ?> <a href="delete.php?id=<?=$posts2_row['post_id'];?>"><i class="fa fa-times-circle-o"></i></a><? } ?>
                                    <? } ?>                                 
                                    </div> 
                                </li>
                                </ul>


                        <? } 
                        }?>
                        </li> 


            <? }
            }

        }
    }
}
?>





                            </ul>





                    </div>
                    </div>

                </div>
            </div>
        </div>

    </div>    
</body>
</html>

对于数据库,我在一个数据库中有帖子:

post_id post_content post_date post_topic file_uploaded post_by post_parent
 1      Parent Topic    07/06     1                       1         0
 2      Child Topic     07/06     1                       1         1
 3       Sub-Child                1                       1         2
 4       New Topic                2                       1         0

因此 Sub-Child 是 Parent Topic 的孙子。 New Topic 是另一个线程,也可以有 child 、曾孙等。

请帮忙。希望你能理解。 谢谢

最佳答案

我知道有两种方法可以将分层数据存储在关系数据库中。第一个是 Adjacency List ,这就像你上面的一样。第二个是Nest Set .使用嵌套集,您可以通过单个查询检索树的整个分支。

无论哪种情况,您都需要一定数量的 recursion从数据库中检索树后,用 PHP 重新创建树。

关于php - 将 2 级回复转换为无限回复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31280487/

相关文章:

PHP Regex 在任意数量的单词和 1-3 位数字之后正确语法 preg_split

javascript - 无法使功能自动刷新

javascript - 将表行 id 附加到 jquery/ajax 函数而不将函数放入循环中

javascript - 从 Javascript 调用 perl 文件/函数和函数来更新 HTML

html - 文本换行时元素占据 100% 宽度 (flexbox)

jquery - 表单输入中的掩码数字仅显示两位小数但保存原始值

mysql - SQL 语句 - SQL 矩阵

mysql - PHP 获取 JSON 并通过 PDO 插入数据库

mysql - 如何更快地加载 vb.net Combobox

javascript - 如何在onClick ="function(string)"中转义字符串