如何像facebook那样在帖子下显示评论?

发布于 2025-01-02 14:44:43 字数 2441 浏览 1 评论 0原文

我正在尝试制作一个类似 facebook wall 的 php 脚本。

它从我的 MySQL 数据库加载帖子内容、照片 url 和用户内容(例如用户名和用户 ID)。它还检查该帖子是否只是对另一个帖子(parentof)的评论。我可以使用标签通过简单的 do-while 来呈现所有带图片的原始帖子。

但我怎样才能在每个帖子下显示评论呢?我想用另一个 do 循环,但是怎么做呢?

这是我的代码:

<?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>

I'm trying to make a facebook wall-alike php script.

It loads the posts stuff, photo url and user stuff like username and user id from my MySQL database. It also checks if the post is just a comment to another post (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?

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.       <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.       <a href="post.php?id=<?php echo $row_wallhaku['post_id']; ?>">Lue, kommentoi</a>
    </td>
    </tr>

    <?php
    }
</table>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

薄凉少年不暖心 2025-01-09 14:44:43

如果帖子之间的关系是一对多,则需要使用 2 个查询。
第一个查询将获取主题(帖子)的数据。
另一个查询将获取“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.
}

In case that the relation between posts is one to many , You need to use 2 queries.
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.

$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.
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文