PHP评论系统,1级回复。查询显示问题
我正在尝试创建一个类似于facebooks wall的PHP评论系统,但每个帖子只有一级回复 我想要这种格式:PHP评论系统,1级回复。查询显示问题,php,mysql,messaging,Php,Mysql,Messaging,我正在尝试创建一个类似于facebooks wall的PHP评论系统,但每个帖子只有一级回复 我想要这种格式: Post 1 Response 1 Response 2 Response 3 Add comment box ---------- Post 2 Response 1 Response 2 Response 3 Add comment box 但是,我的代码目前产生了以下结果: Post 1 Response 1 Add comment box Post 1 Resp
Post 1
Response 1
Response 2
Response 3
Add comment box
----------
Post 2
Response 1
Response 2
Response 3
Add comment box
但是,我的代码目前产生了以下结果:
Post 1
Response 1
Add comment box
Post 1
Response 2
Add comment box
----------------
Post 2
Response 1
Add comment box
---------------
Post 2
Response 1
Add comment box
---------------
我想循环浏览结果,这样每次循环时,帖子消息不会与该帖子相关行中的下一条评论一起打印。因此,它应该在主消息上循环一次,打印它,然后通过循环在下面发布所有相应的注释
表结构
帖子
=该职位的编号p_id
=他们发布的用户user\u id
=发布帖子的人poster\u user\u id
post
=该注释的编号注释\u id
=与评论相关的帖子编号post\u id
=发表评论的人commenter\u user\u id
注释
while ($query_row = mysql_fetch_assoc($query_run)){
// more variable definitions
$post_id = $query_row['post_id']
echo " <form method=\"post\" action=\"add_comment.php?post_id='.$post_id.'>
<textarea name=\"comment\"></textarea> <br>
<input type=\"submit\" value=\"Send\" /></form> "
while($query\u row=mysql\u fetch\u assoc($query\u run)){
//更多变量定义
$post\u id=$query\u行['post\u id']
echo“递归怎么样?它适合您的需要吗?如果是,那么您只需要每条消息的父消息id(如果不是响应,则为NULL)。然后加载所有消息并仅使用空消息调用函数,同时提供整个消息数组。代码的问题是,对于从数据库中读取的每个注释,您也会从连接中获得帖子。我现在可以想到两种可能的解决方案:
第一个选项是保持查询的原样,但无论何时打印一行,您都会查看打印的帖子。您将存储上次打印的帖子id,如果当前id相同,您只需打印评论而不打印帖子。这显然需要拆分帖子和评论的标记(无论如何,这是个好主意)。基本上是这样的:
$lastPost = -1;
while ($query_row = mysql_fetch_assoc($query_run)) {
// ...
if ($pid != $lastPost) {
echo "<code for the post>";
$lastPost = $pid;
}
echo "<code for the comment>";
}
$query_run = // query only for posts, ignoring comments
while ($query_row = mysql_fetch_assoc($query_run)) {
// ...
echo "<code for the post>";
$comment_query = // query only for comments with post_id = $pid
while ($comment_row = mysql_fetch_assoc($comment_query)) {
// ..
echo "<code for the comment>";
}
}
更新
有多种方法可以在表单中包含信息,然后将其传递到目标页面。一种方法是在您自己尝试时将其作为GET参数包含在URL中。另一种方法是包含仅保存静态数据的隐藏输入元素。无论您使用什么,您都必须从$_在目标页面上获取
或
$\u发布。在一些输入验证之后(您应该这样做),然后您可以使用这些值插入注释,就像您通常只使用一个表单一样,只是post id也来自一个请求变量。如果您不知道如何正确处理用户输入和创建注释,我建议您开始稍微慢一点,现在只制作一个注释表单,以了解它的工作原理ks
“未定义索引”错误意味着您试图访问数组中的索引(可能是
$query\u行
)这是不存在的。您只能访问sql查询中实际请求的值。因此,请检查您尝试访问的变量是否实际存在于查询中。这是我预期会发生的情况,因为您的sql查询正在从两个表中进行选择,这是一个内部联接,因此您将获得重复的行
直接在mysql
query窗口中尝试查询,看看结果如何
我建议存储当前帖子的值,仅在帖子未更改时输出评论;类似于以下内容:
另外,您没有在表单中记录post id(作为隐藏变量或URL),因此我添加了一个隐藏变量,因为没有post_id,我怀疑这将是您的下一个bug
$last_post_id = null;
while ($query_row = mysql_fetch_assoc($query_run)){
$post_id = $query_row['post_id'];
if ($last_post_id == $post_id)
{
?>
<ul class=\"comment\">
$commenter_user_id
<li> $comment </li>
</ul>
<?php
}
else
{
if ($last_post_id !== null)
{
?>
<form name=\"message\" method=\"post\" action=\"sendmessage.php\">
<textarea name=\"message\"></textarea> <br>
<input type=\"hidden\" name="\post_id\" value=\"$last_post_id\" /> </form>
<input type=\"submit\" value=\"Send\" /></form>
<?php
$last_post_id = $post_id;
}
?>
<div id=\"post\">
Post $pid $post_id Poster: $poster_id Mentions: $user_id
<br><br> $message <br><br>
<ul class=\"comment\">
$commenter_user_id
<li> $comment </li>
</ul>
</div>
<?php
}
?>
格式不正确。回复应该在帖子下面。同样,类似于facebook甚至stackoverflow。只是一个一般性的提示:为了表达你的感激之情,请投票选出对你有帮助的答案,并最终接受回答你问题的答案。谢谢你戳!我现在还不能投票,因为我没有投票权声誉。但是我选择了你的答案。谢谢你的帮助。你应该能够对自己的问题进行投票表决;事实上,你现在拥有的16个声誉应该足够了,所以也许可以再试一次?:)你会用递归来做什么?如果需要的话,它会很容易扩展tho和代码(即使只在一个关卡上使用它)也很容易。1>2是因为我认为速度:)非常感谢你的帮助!很有效!你能用另一个问题查看我更新的帖子吗?谢谢你。@RickJoe为什么会错?你需要为每个帖子获取评论。只是问一下……因为我需要这样的系统,在我的工作中,我们不允许在循环中选择。感谢Richard修改表单me!我想你可能有回答了我更新后的问题。您能否更新您的答案以包含post_id变量,以便我在发布新评论时可以更新评论表?谢谢!@user1463075我已更新了我的答案,以便为您提供如何进行基本表单处理的指导。
$query_run = // query only for posts, ignoring comments
while ($query_row = mysql_fetch_assoc($query_run)) {
// ...
echo "<code for the post>";
$comment_query = // query only for comments with post_id = $pid
while ($comment_row = mysql_fetch_assoc($comment_query)) {
// ..
echo "<code for the comment>";
}
}
$last_post_id = null;
while ($query_row = mysql_fetch_assoc($query_run)){
$post_id = $query_row['post_id'];
if ($last_post_id == $post_id)
{
?>
<ul class=\"comment\">
$commenter_user_id
<li> $comment </li>
</ul>
<?php
}
else
{
if ($last_post_id !== null)
{
?>
<form name=\"message\" method=\"post\" action=\"sendmessage.php\">
<textarea name=\"message\"></textarea> <br>
<input type=\"hidden\" name="\post_id\" value=\"$last_post_id\" /> </form>
<input type=\"submit\" value=\"Send\" /></form>
<?php
$last_post_id = $post_id;
}
?>
<div id=\"post\">
Post $pid $post_id Poster: $poster_id Mentions: $user_id
<br><br> $message <br><br>
<ul class=\"comment\">
$commenter_user_id
<li> $comment </li>
</ul>
</div>
<?php
}
?>
<?php
if (isset($_POST['post_id']))
{
$post_id = mysql_real_escape_string($_POST['post_id']);
$user_id = $_SESSION['current_user_id'];
$sql = "INSERT INTO `comments`SET
`post_id` = $post_id
'commenter_user_id = $user_id";
if (!mysql_query($sql))
echo "MySQL error during insert ". mysql_error();
}
else
{
echo "Invalid form posting";
}