2

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: 変数が返されます。

どうすればこれを達成できるかについて何か考えはありますか?

4

3 に答える 3

4
于 2012-06-18T09:17:53.970 に答える
1

これは、SQL クエリが内部結合である 2 つのテーブルから SELECT しているため、私が期待していたことです。そのため、行の重複が発生します。

mysqlクエリ ウィンドウで直接クエリを実行して、何が表示されるかを確認してください。

現在の投稿の値を保存し、投稿が変更されていない場合にのみコメントを出力することをお勧めします。次のようなもの:

また、投稿 ID をフォーム (隠し変数または URL のいずれか) に記録していないため、次のバグになると思われる post_id がないため、隠し変数を追加しました。

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

更新された回答

sendmessage.php(入力タイプ=非表示で上記のフォームを使用)、以下を参照してください。投稿目的で現在のユーザー ID を取得するには、セッションを使用する必要があることは間違いありません。

データを変更するフォームを送信するには、POST メソッドを使用することをお勧めします。

<?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";
}
于 2012-06-18T09:21:06.153 に答える
0

What about recursion? Does it suit your needs? If yes, then all you need is the parent message id to each message (or NULL if it is not a response). Then you load all the messages and call the function with NULL messages only, also providing the whole array of messages.

于 2012-06-18T09:07:57.167 に答える