Facebookの壁に似たPHPコメントシステムを作成しようとしていますが、投稿ごとに1レベルの返信しかありません。
私はこのフォーマットが欲しいです:
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
---------------
結果をループして、ループするたびにその投稿に関連する行の次のコメントと共に投稿メッセージが印刷されないようにします。そのため、メイン メッセージを 1 回ループして印刷し、対応するすべてのコメントを同様にループしてその下に投稿する必要があります。
テーブル構造
投稿p_id
= その投稿の数user_id
= 投稿しているユーザーposter_user_id
= 投稿者post
comment_id
= そのコメントの数post_id
= コメントが関連する投稿の番号commenter_user_id
= そのコメントをしている人物comment
コード
$query = "SELECT posts.p_id, comments.post_id, posts.poster_id,
posts.user_id, message, `comments.commenter_user_id, comments.comment`
FROM posts, comments
WHERE posts.p_id = comments.post_id";
$query_run = mysql_query($query) or die(mysql_error());
while ($query_row = mysql_fetch_assoc($query_run)) {
$pid = $query_row['p_id'];
$post_id = $query_row['post_id'];
$poster_id = $query_row['poster_id'];
$user_id= $query_row['user_id'];
$message = $query_row['message'];
$commenter_user_id = $query_row['commenter_user_id'];
$comment = $query_row['comment'];
echo "
<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>
<form name=\"message\" method=\"post\" action=\"sendmessage.php\">
<textarea name=\"message\"></textarea> <br>
<input type=\"submit\" value=\"Send\" /></form>
</div>
<br>";
}
望ましい結果を得る方法について何かアドバイスはありますか? ロジックを変更するだけでこれを簡単に修正できる可能性があることはわかっていますが、学習を始めたばかりです。これに関するあなたの助けは大歓迎です!
アップデート
助けてくれてありがとう。コード例の側面を取り入れましたが、完全に機能します。
もう1つ質問があります:
上記のように、それぞれにコメントが含まれています。コメントが投稿されたら、コメント テーブルを新しい投稿で更新したいと思います。コメントするときに特定の投稿ごとに post_id と commenter_user_id 変数を取得して、そのデータを使用してコメントテーブルへの挿入クエリを次のように実行するにはどうすればよいですか。
INSERT INTO `comments`SET
`post_id` = $post_id
'commenter_user_id = $_SESSION['commenter_user_id'] " (I was thinking sessions?)
URLを介して変数をスクリプトページに渡すことで、データを取得することを考えていました。
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> "
しかし、次のように変数を他のページに渡そうとしました: action=\"add_comment.php?post_id='.$post_id.' しかし、それを行おうとすると、未定義の index: 変数が返されます。
どうすればこれを達成できるかについて何か考えはありますか?