1

私のウェブサイトには、コメントを許可する簡単な質問と回答のセクションがあります。現在、ループと$_GET['id']値を使用して回答を入力しています。これが私の質問です

<?php
try{
   $parent=$_GET['id'];
   $post_type = "2";
   $stmt = $dbh->prepare(
    "SELECT p.post_id, p.content, p.author, p.created, pc.comment_id AS comment_id, pc.content AS comment_content
     FROM posts AS p
     LEFT JOIN posts_comments AS pc
     ON p.post_id = pc.parent_id
     WHERE p.post_type = :post_type AND  p.parent = :parent ");

   $stmt->bindParam(':parent', $parent, PDO::PARAM_INT);
   $stmt->bindParam(':post_type', $post_type, PDO::PARAM_INT);
   $stmt->execute();

   $answers_array = array();
   while ($answers = $stmt->fetch(PDO::FETCH_ASSOC)) {
        $answers_array[]= $answers;
   }
}

?>

これにより、次の結果が配列に返されます

   Array ( 
[0] => Array ( 
        [post_id] => 12 
        [content] => I have an answer
        [author] => Author1
        [created] => 2012-06-09 21:43:56
        [comment_id] =>
        [comment_content] => ) 
[1] => Array ( 
        [post_id] => 13
        [content] => My second answer
        [author] => Author1
        [created] => 2012-06-10 06:30:58
        [comment_id] => 35
        [comment_content] => 1st comment ) 
[2] => Array ( 
        [post_id] => 13
        [content] => My second answer
        [author] => Author2
        [created] => 2012-06-10 06:30:58
        [comment_id] => 36
        [comment_content] => 2nd comment ) 
[3] => Array ( 
        [post_id] => 13
        [content] => My second answer
        [author] => Author2 
        [created] => 2012-06-10 06:30:58
        [comment_id] => 37
        [comment_content] => 3rd comment ) 
)

私のquestion.phpページでは、これらの結果を次のようなものでループする必要があることを知っています。

<?php $answer_count = count($answers_array);
 for($x = 0; $x < $answer_count; $x++) {
 $answers = $answers_array[$x];
}
?>

私の質問 -

2つの別々のクエリを使用して各回答に関連付けられたコメントを取得するか、上記の結合を使用してphpで別の配列を作成するかはわかりません。正直にそれをする方法がわかりません。結合を使用する場合、このような配列が必要ですが、phpのループを使用してそれを構築する方法がわかりません。

     Array ( 
[0] => Array ( 
        [post_id] => 12 
        [content] => I have an answer
        [author] => Author1 
        [created] => 2012-06-09 21:43:56 
        [comment_id] => 
        [comment_content] => ) 
[1] => Array ( 
        [post_id] => 13 
        [content] => My second answer
        [author] => Author1 
        [created] => 2012-06-10 06:30:58 
            Array( 
                   [1] => Array (
                       [comment_id] => 35
                       [comment_content] => 1st comment)
                   [2] => Array (
                       [comment_id] => 36
                       [comment_content] => 2nd comment)
                   [3] => Array (
                       [comment_id] => 37
                       [comment_content] => 3rd comment))

そのような配列ができたら、question.phpページの元のphpループ内でそれぞれに対してを実行できると考えていました。

4

3 に答える 3

2

クエリは 1 つだけで結構です。あなたが持っているように、おそらくより良いオプションです。MySQL に負荷をかけるか、ネットワークと PHP に負荷をかけるか、どちらがより効率的かを検討する必要があります。MySQL よりも PHP に負担を負わせた方がはるかに良いのですが、MySQL に必要なグループ化などの「組み込み」機能がある場合は、MySQL を離れてネットワーク トラフィックを節約します。

これを機能させるには、「ORDER BY p.post_id, pc.comment_id」をクエリに追加します。これにより、結果が順番に取得されます。

次に、配列に組み込む必要がある場合 (配列を使用せずに直接処理できる場合もありますが、方法は同様です):

$lastPostID = 0;
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    if ($lastPostID <> $row['post_id']) {
        $lastPostID  = $row['post_id'];
        $answers[$lastPostID] = array('post_id' => $row['post_id'],
                                      'author_id' => $row['author_id'],
                                      etc
                                      'comments' => array() );
    }
    $answers[$lastPostID]['comments'][] = array('comment_id' => $row['comment_id'], 'coment' => $row['comment'] etc);
}
于 2012-06-23T01:02:27.613 に答える
1

選択はあなた次第です。それぞれに長所と短所があります。単一のクエリ (明らかに単一のデータベース呼び出し) を使用すると、データ セットを 1 回だけループする必要がありますが、重複したデータが返される可能性があります。複数のクエリを使用すると、必要なデータのみを正確に取得できますが、(明らかに、複数のデータベース呼び出しと) 複数のデータ セットを反復処理する必要があります。

以下は、単一クエリ メソッドのダーティな実装です (コメントの作成が独自のループであることを除いて、同じ基本的なフローが複数クエリ メソッドに適用されます)。しかし、基本的な考え方はそこにあります。回答のマップがあり、レコードを反復するときにそれらを追加/取得し、コメントを追加します。

while ($answers = $stmt->fetch(PDO::FETCH_ASSOC)) 
{
    $post_id = strval($answers['post_id']);
    if(!array_key_exists($post_id, $answers_array)) {
        $answers_array[$post_id] = array(
            'post_id' => $answers['post_id'],
            // ... assign other stuff ...
            'comments' => array()); //initialize the comments array
    }
    if(!empty($answers_array['comment_id'])) {
        $obj = $answers_array[$post_id];
        $obj['comments'][] = array(
            'comment_id' => $answers['comment_id'], 
            'comment_content' => $answers['comment_content']);
    }
}
于 2012-06-23T00:58:57.543 に答える
0

JOIN を使用して、単一のクエリですべての結果を返すことをお勧めします。そうしないと、投稿ごとに 1 つずつ、複数のクエリを作成することになります。多くの個別のクエリを発行すると、多くのオーバーヘッドが発生します。

ごく少数の投稿結果を返したい場合は例外です。

post_id、comment_id でクエリを並べ替えてください。

結合された結果を反復処理するには、次のようになります。

$post_id = 0;
foreach($rows as $row) {
  // See if our post changed
  if ($post_id != $row['post_id']) {
    // Display the post and post header
    ..
    // Update the current post_id variable
    $post_id = $row['post_id'];
  }
  // Display the comment
}
于 2012-06-23T00:55:13.020 に答える