简体   繁体   English

如何在像Facebook这样的帖子下显示评论?

[英]How to show comments under a post like facebook does?

I'm trying to make a facebook wall-alike php script. 我正在尝试制作一个类似Facebook Wall的php脚本。

It loads the posts stuff, photo url and user stuff like username and user id from my MySQL database. 它从我的MySQL数据库加载帖子内容,照片url和用户内容,例如用户名和用户ID。 It also checks if the post is just a comment to another post (parentof). 它还会检查该帖子是否只是对另一个帖子(parentof)的评论。 I can present all the original posts with pictures with a simple do-while using tags . 我可以在使用标签的同时通过简单的操作向所有原始帖子展示图片。

But how could I present the comments under every post? 但是我该如何在每个帖子下发表评论? I guess with another do-loop, but how? 我猜想有另一个do-loop,但是如何?

Here is my code: 这是我的代码:

<?php

require_once "config.php";
    $result = mysql_query('SET NAMES utf8');
    $result = mysql_query('SET CHARACTER SET utf8');
    $wallhaku = mysql_query("SELECT `wall`.`post_id`, `wall`.`parentof`, `wall`.`sentby`, `wall`.`text`, `wall`.`image_url`, `users`.`username`, `users`.`photopath`, `users`.`name`, `users`.`member_id` FROM `wall` LEFT JOIN `users` ON `wall`.`sentby` = `users`.`member_id` WHERE parentof=0 ORDER BY post_id DESC") or die (mysql_error());
    $row_wallhaku = mysql_fetch_array($wallhaku);


<table width="800" height="120" border="0" cellpadding="10">
<?php $i=0; $numberpage=1;
    do {
    $wallid = $row_wallhaku['post_id'];
    $parenthaku = mysql_query("SELECT post_id, parentof FROM wall WHERE parentof=$wallid") or die (mysql_error());
    $row_parenthaku = mysql_num_rows($parenthaku);

      <td width="120" align="left">
      <img src=<?php echo $row_wallhaku['photopath']; ?> height=    "100" width="100">
      </td>
      <td width="600" align="left">
    <a href="member.php?id=<?php echo $row_wallhaku['member_id']; ?>"><?php echo $row_wallhaku['name']?></a><br /><p><?php echo $row_wallhaku['text'];
    if($row_wallhaku['image_url']=="0") {

    <p align="right"><?php echo $row_parenthaku ?> kommenttia.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="post.php?id=<?php echo $row_wallhaku['post_id']; ?>">Lue, kommentoi</a> <?php } ?></td>
<?php $i++; if($i%$numberpage==0) echo "</tr>";
if($row_wallhaku['image_url']!="0")
    {

    <tr>
    <td width='800' colspan='2' align='center'>
    <a href="post.php?id=<?php echo $row_wallhaku['post_id']; ?>"><img src=<?php echo $row_wallhaku['image_url']; ?> height="180"></a>
    <p align="right"><?php echo $row_parenthaku ?> kommenttia.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="post.php?id=<?php echo $row_wallhaku['post_id']; ?>">Lue, kommentoi</a>
    </td>
    </tr>

    <?php
    }
</table>

In case that the relation between posts is one to many , You need to use 2 queries. 如果帖子之间的关系是一对多,则需要使用2个查询。 The first query will get the data of the topic (the post). 第一个查询将获取主题(帖子)的数据。 The other query will get the data of all the comments which "parentof" equals the topic's id. 另一个查询将获取所有“ parentof”等于主题ID的评论的数据。

$id = $_GET['id'];
$getTopic = mysql_query("SELECT * FROM posts WHERE id='$id'");
$topic = mysql_fetch_array($getTopic);

$getComments = mysql_query("SELECT * FROM posts WHERE parentOf='{$topic['id']}'");
while($comment = mysql_fetch_array($getComments))
{
 echo $comment['title']; //You should use a "view" functin which handles the HTML and staff.
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM