在php oop中从重复停止mysql值?

时间:2014-09-09 20:14:46

标签: php mysql

我正在从MYSQL生成一个循环问题。我希望question_category不要在每一行上复制,只在该类别的第一个问题上显示一次?

现在:
问题类别1问题1(O)(O)(O)
问题类别1问题2(O)(O)(O)
问题类别2问题1(O)(O)(O)
问题类别2问题2(O)(O)(O)

我的目标:
问题类别1 问题1(O)(O)(O)
问题2(O)(O)(O)
问题类别2 问题1(O)(O)(O)
问题2(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>

3 个答案:

答案 0 :(得分:1)

由于您的查询显然会返回已订购的内容,因此特定类别的所有问题都会组合在一起....

<?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>';
}
?>

答案 1 :(得分:0)

您可以执行查询"SELECT DISTINCT question_category FROM questions"并在该循环中执行"SELECT * FROM questions where active=1 AND question_category='$row->question_category' AND question_sort=1 ORDER BY sort_by"查询

答案 2 :(得分:0)

一种方法是

$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;
...

这假定您的查询当然是按顺序在某个阶段按问题_category排序