2012-06-22 21 views
1

我在我的网站上有一个简单的问答部分,允许评论。我目前使用循环和$ _GET ['id']值填充答案。这里是我的查询通过MySQL循环左加入PHP与2个单独的查询

<?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]; 
} 
?> 
以下结果

我的问题 -

我不知道是否使用两个单独的查询来拉t他评论与每个答案相关联或使用我上面的连接并使用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循环中的数组。

+0

它可以帮助更多的来发表您的架构片段比它确实发布了大量的示例输出。 –

+0

我缩短了输出。我不确定需要多少才能回答这个问题。我会努力将模式放在问题中而不是输出 – FajitaNachos

回答

2

一个查询很好。就像你拥有它,并且可能是更好的opton。你必须弄清楚哪一个更高效,让MySQL承受压力,或者网络和PHP承受压力。让PHP比MySQL承受压力要好得多,但是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); 
} 
+0

谢谢。这很好。我必须将第2行的$答案更改为$ row,但它给了我预期的结果。现在我将使用每个循环的嵌套来提取数据。 – FajitaNachos

+0

啊 - 抱歉,错误和良好的捕获。我会很快修复,以防其他人复制它!祝好运与项目的其余部分。 – Robbie

0

您将要使用JOIN以单个查询返回所有结果,否则,您将进行多个查询,每个帖子一个查询。通过发出许多单独的查询,您会有很多开销。

如果您想返回非常少量的帖子结果,则会出现异常。

请确保通过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 
} 
+0

感谢您的回答。我将继续我在这个问题上的加入。你知道如何通过循环遍历php中的结果来将当前数组返回到第二个多维数组中吗? – FajitaNachos

+0

@FajitaNachos,我更新了我的答案,详细介绍了我过去如何完成这样的事情。我不认为你需要一个单独的数组。 – Ami

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']); 
    } 
}