仅从阵列显示一次

时间:2016-02-13 11:32:12

标签: php mysql mysqli

我使用以下代码。它通过数据库并提出问题和相关答案。

但每次打印答案时都会显示问题。

如何制作,以便只打印一次问题,然后打印答案。

$qna = mysqli_query($conn, "SELECT q.QText, a.id, a.AText FROM question q INNER JOIN answer a ON q.id = a.Question_ID WHERE q.id=1")  or die(mysqli_error($conn));

while ($data2 = mysqli_fetch_array($qna)) {

echo '<td>'.$data2['QText'].'</td>';

echo '

<table class="layout display responsive-table">
        <thead>
            <tr>
                <th>ID</th>
                <th>Answer</th>
            </tr>
        </thead>
    <tr>
    <td>'.$data2['id'].'</td>
    <td>'.$data['AText'].'</td>
    <td>'.$data['Group_ID'].'</td>
    </table>

';

  }

1 个答案:

答案 0 :(得分:1)

从问题表中选择一个ID,如:

SELECT q.QText, q.id AS QId, a.id, a.AText FROM ...

然后您只能使用条件打印出一次问题:

$lastQuestionID = 0;
while ($data2 = mysqli_fetch_array($qna)) {

if($data2['QId'] != $lastQuestionID)
    echo '<td>'.$data2['QText'].'</td>';

$lastQuestionID = $data2['QId'];

// printing answer here

}