1

I have a forum that students are posting in. If the student responds to the main post, the postId is set to 0. If a student replies to another student's post, the postId is set to the replyId of the student's original post.

I am trying to write some PHP that will essentially create a new table for each post, with the exception of if a postid is set to a reply Id, then create a new row in that table.

I have the SQL laid out in SQLFiddle which can be found here:

http://sqlfiddle.com/#!2/611e2d/5

Using this example, what I'm looking for is replyid one to be put in a new html table, with response ID 3 in a new row underneath that. Then with ReplyId 2, would create a new html table.

This is just a basic threaded forum view of responses.

Thank you!

[code]

    $i = 0;
    $aR = 0;
    $currentPost = '';

    if(mysql_num_rows($getResponses) > 0)
        {
        while($respData = mysql_fetch_array($getResponses))
            {

            if(($respData['postId'] != $currentPost)) 
                {
                if($i!=0)  
                    {  
                    echo '</table><br /><br />';            
                    } 


                echo '<table width = "875px" cellspacing = "0" cellpadding = "0" border = "0">';

                $i=0;
                }

        $currentPost = $respData['postId'];

        $color_A = 'class="altRow1"'; 
        $color_B = 'class="altRow2"';

        $altRowColor = ($aR % 2) ? $color_A : $color_B;

        $studentName = getStudents($respData['userId']);
        $studentName = explode(" ", $studentName);
        $studentFirstName = $studentName[0];

        echo '<tr ' . $altRowColor . '>
                <td align="center" width = "225px" class="forumTopic"><img src="images/'.getStudentPics($respData['userId']).'.png" /><br />Posted By ' . getStudents($respData['userId']) . '<br />on '.date("m/d/Y h:i a", strtotime($respData['responseDate'])) . '</td>
                <td width = "650px" class="forumTopic">' . $respData['replyText'] . '</td>
            </tr>
            <tr ' . $altRowColor . '>
                <td class="forumTopic" colspan = "2" align="center"><span class="topicLinkStyle"><a href="postResponse.php?postId='.$respData['replyId'].'&topic='.$topicId.'" class="iframe750x600">Reply to '.$studentFirstName .'</a></span></td>
            </tr>';


        $i++;
        $aR++;
        }

    echo '</table><br /><br />';
    }
4

2 回答 2

2

这是一个非常简单的示例,然后您可以制作自己的 CSS 来格式化 HTML,您甚至可以使用我在您的评论中发布的链接作为示例

buildTree将以树的方式正确地对回复进行排序,以便以后使用,并将printTree递归打印。

<?php    
// Your database info
$db_host = '';
$db_user = '';
$db_pass = '';
$db_name = '';

if (!isset($_GET['topic_id']))
{
    die('No topic id was given.');
}

$con = new PDO("mysql:host={$db_host};dbname={$db_name}", $db_user, $db_pass);
$con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$sql = "SELECT replyId,
               topicId,
               postId,
               replyText,
               responseDate,
               userId 
          FROM forumResponses 
         WHERE topicId = ? 
      ORDER BY replyId";
$result = $con->prepare($sql);
$result->bindParam(1, $_GET['topic_id'], PDO::PARAM_INT);
$result->execute();

if ($result->rowCount() == 0)
{
    die('No messages found...');
}

$threads = array();
while($row = $result->fetchALL(PDO::FETCH_ASSOC))
{
    $threads = $row;
}

$data = buildTree($threads);

$con = NULL;

function buildTree($ar, $pid = NULL) {
    $op = array();
    foreach($ar as $item)
    {
        if($item['postId'] == $pid)
        {
            $op[$item['replyId']] = array(
                'replyText' => $item['replyText'],
                'userId' => $item['userId'],
                'responseDate' => $item['responseDate'],
                'parentId' => $item['postId']
            );
            // using recursion
            $children =  buildTree($ar, $item['replyId']);
            if($children)
            {
                $op[$item['replyId']]['children'] = $children;
            }
        }
    }
    return $op;
}

function printTree($ar)
{
    foreach ($ar as $reply)
    {
?>
        <li>
          <div>
            <header><a href="javascript:void(0);">userId <?php echo $reply['userId']; ?></a> - <?php echo $reply['responseDate']; ?></header>
            <?php echo $reply['replyText']; ?>
          </div>
<?php
        if (isset($reply['children']) && count($reply['children']) > 0)
        {
             echo "     <ul>\n";
             printTree($reply['children']);
             echo "     </ul>\n";
        }
        echo "        </li>\n";
    }
}

?>
<!doctype html>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
  <title>Threaded Comments Block</title>
</head>

<body>
  <div>
    <h1>Reading Topic <?php echo $_GET['topic_id']; ?></h1>
    <div>
      <ul>
<?php
printTree($data);
?>
      </ul>
    </div>
  </div>
</body>
</html>

注意:上面的代码只是向您展示如何以线程方式存储结果以及打印结果的示例,您必须在此示例中制作自己的代码。

于 2013-09-03T05:12:56.073 回答
0

我认为为此重建表格会更好..hmm 这是我的建议 Table Post(建议主表用于主题的响应)

领域:

postId, topicId(这个帖子所属的主题), responseId(如果这个帖子是回复,这将是帖子的postId,如果是主帖子,则设置为0),

于 2013-09-03T03:52:14.630 回答