Comment for each post id without multiplication of it

0

First look at my database image (the link arrows from one table to another are inner join 's): link

Current Query:

"SELECT
question.*,
questioncomments.*,
login.*
FROM questioncomments
INNER JOIN question
ON questioncomments.comment_question_ID = question.question_ID
INNER JOIN login
ON question.autor_id = login.user_ID
ORDER BY question.question_ID";

Part of my PHP code:

<?php
 while($row = $result->fetch_assoc()) {
     echo "<hr>[Row 'titulo'] TITLE: " . $row["titulo"] . "<br><br>QUESTION: " . $row["question"] . "<br><br>COMMENT:  " . $row["comment"] . "<br><br>"; 
}
?>

Question:

How to have the comments included in each question_id instead of multiplying each question with each answer?

Expected result (formerly see poor result):

  

TITLE: Please help me

     

QUESTION: What is GOOGLE?

     

COMMENT: is a searcher

     

COMMENT: is a game

Bad result:

  

TITLE: Please help me

     

QUESTION: What is GOOGLE?

     

COMMENT: is a searcher

     

TITLE: Please help me

     

QUESTION: What is GOOGLE?

     

COMMENT: is a game

    
asked by anonymous 21.04.2015 / 21:31

1 answer

1
// VARIAVEL DE CONTROLE PARA TROCA DE QUESTÃO.
$idControle = 0;
while($row = $result->fetch_assoc()) {
    // A QUESTÃO ATUAL É DIFERENTE DA QUESTÃO ANTERIOR? SE FOR IMPRIME CABEÇALHO.
    if($idControle != $row['question_ID']){
        echo "<hr> TITLE: " . $row["titulo"] . "<br><br>QUESTION: " . $row["question"]."    <br>";
    }
    echo "<br>COMMENT:  " . $row["comment"] . "<br>"; 

    // ATUALIZA VARIAVEL DE CONTROLE.
    $idControle = $row['question_ID'];
}
    
21.04.2015 / 22:29