Comment for each post id without multiplication of the same

Asked

Viewed 67 times

0

First see the image of my database (the link arrows from one table to another are the inner join's): http://prntscr.com/6wjl4h

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 (before see the bad 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

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'];
}
  • vo try this , I have not gotten anything yet because of the errors of your code

  • THANK YOU <3 YOU ARE A GENIUS!!!!!! 11

  • Could you give me a translated summary of what this code does ? It worked perfectly, thank you

  • I created a control variable $idControle that starts with 0. In the first iteration of while let’s assume that the question_ID is 1, the IF will check if (0 != 1), if it is it will print the issue header and then the first comment, then $idControle gets 1. In the next iteration it will only print the header of the question again if the question_ID is different from 1. ?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.