回声从sql形式正确的方式?

时间:2014-08-22 20:56:16

标签: php echo

我正在尝试回复这样的表格:

          1   2   3
Question (o) (o) (o)
Question (o) (o) (o)

但我最终得到了这个:

             1   2   3
  Question  (o) (o) (o)
             1   2   3
  Question  (o) (o) (o)

     if ($result) {
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
    $body = $row['question_body'];
    $question_id = $row['question_id'];
        echo '<div class="questions-1">';
        echo '  
        <table width="auto" border="0">
          <tr>
            <td>&nbsp;</td>
            <td class="answer_value">1</td>
            <td class="answer_value">2</td>
            <td class="answer_value">3</td>
          </tr>
          <tr>
            <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>';      
         echo '</div>';  

2 个答案:

答案 0 :(得分:5)

<!-- try to not echo big chunk of text, instead remove them out of the php section of code -->
<div class="questions-1">  
        <table width="auto" border="0">
            <tr>
                <td>&nbsp;</td>
                <td class="answer_value">1</td>
                <td class="answer_value">2</td>
                <td class="answer_value">3</td>
              </tr>
    <?php
    if ($result) {
    while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
        $body = $row['question_body'];
        $question_id = $row['question_id'];
              echo '<tr>
                <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>

答案 1 :(得分:0)

尝试在while语句之前打印标题

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