当前位置: 动力学知识库 > 问答 > 编程问答 >

Stop mysql value from duplicate in php oop?

问题描述:

I'm generating questions from MYSQL into a loop. I would like for the question_category not to duplicate on every row, just displayed once on top of first question in that category?

Right now:

Question category 1 Question1 (O) (O) (O)

Question category 1 Question2 (O) (O) (O)

Question category 2 Question1 (O) (O) (O)

Question category 2 Question2 (O) (O) (O)

My goal:

Question category 1

Question1 (O) (O) (O)

Question2 (O) (O) (O)

Question category2

Question1 (O) (O) (O)

Question2 (O) (O) (O)

<?php

// generate active questions from DB

$query = "SELECT * FROM questions where active=1 AND question_sort=1 ORDER BY sort_by";

$result = @mysqli_query($con, $query);

?>

<div class="questions-1">

<table width="auto" border="0">

<tr>

<td></td>

<td></td>

<td class="answer_value">0</td>

<td class="answer_value">1fta</td>

<td class="answer_value">2</td>

</tr>

<?php

if ($result) {

while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {

// question body for each question

$body = $row['question_body'];

// question id for each question

$question_id = $row['question_id'];

// question category for each question

$question_category = $row['question_category'];

echo '<tr>

<td class="question">'.$question_category.'</td>

<td class="question">'.$body.'</td>

<td class="answer"><input type="radio" name="answer_value['.$question_id.']" value="0" ></td>

<td class="answer"><input type="radio" name="answer_value['.$question_id.']" value="1" ></td>

<td class="answer"><input type="radio" name="answer_value['.$question_id.']" value="2" ></td>

</tr>';

}

?>

</table></div>

网友答案:

Since your query apparently returns things already ordered so that all questions of a specific category are grouped together....

<?php
$currcategory = ""; // Holder to keep track of the current category we are working with.
if ($result) {
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
    // question body for each question
    $body = $row['question_body'];
    // question id for each question
    $question_id = $row['question_id'];
    // question category for each question
    $question_category = $row['question_category'];
    if ($currcategory !== $question_category) { // The category has changed so lets create a 'header' row.
        $currcatgory = $question_category;
        echo '<tr><td colspan="5">'.$currcategory.'</td></tr>';
    }
          echo '<tr>
            <td class="question">'.$question_category.'</td>
            <td class="question">'.$body.'</td>
            <td class="answer"><input type="radio" name="answer_value['.$question_id.']" value="0" ></td>
            <td class="answer"><input type="radio" name="answer_value['.$question_id.']" value="1" ></td>
            <td class="answer"><input type="radio" name="answer_value['.$question_id.']" value="2" ></td>
          </tr>';
}
?>
网友答案:

You could do a query "SELECT DISTINCT question_category FROM questions" and in that loop do your "SELECT * FROM questions where active=1 AND question_category='$row->question_category' AND question_sort=1 ORDER BY sort_by" query

网友答案:

One way would be

$cur_question_category = '';

Then in your loop, if($question_category != $cur_question_category) { 
// display the category
}
else
{
//do not display the category
}
$cur_question_category=$question_category;
...

This assumes of course your query is ordering by question_category at some stage in the order by

分享给朋友:
您可能感兴趣的文章:
随机阅读: