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

时间:2015-07-07 22:31:17

标签: 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是父主题的大孩子。新话题是另一个主题,也可以有孩子,大孩子等。

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

1 个答案:

答案 0 :(得分:0)

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

在任何一种情况下,在您从数据库中检索到树之后,您需要一些recursion来重新创建PHP树。