2012-06-22 20 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']); 
    } 
}