想要正确显示html表中的数据

时间:2012-11-05 01:12:14

标签: php html sql mysqli

我有一个关于如何在我的PHP代码中显示正确的表格布局的问题:

我想在表格中显示答案及其文字输入。现在,它显示如下:

QuestionNo   Question             Answer        Marks Per Answer     
1            What is 2+2          B             (text input)         
2            Name the 3 hobbits?  BCE           (text input)  

我想更改表格的显示,使其如下所示:

 QuestionNo  Question             Answer        Marks Per Answer 
 1           What is 2+2?          B             (text input)
 2           Name the 3 Hobbits?   B             (text input)                   
                                   C             (text input)
                                   E             (text input) 
  1. 正如您从新显示屏中看到的那样。我希望每个问题的每个答案都显示在他们自己的行中,而不是每个问题的所有答案都在一行中,这就是它正在做的事情。
  2. 我希望文本输入也显示在自己的行中,如答案:
  3. 我的问题是如何实现第1点和第2点以便它可以匹配新的布局?

    以下是当前显示的代码:

      <?php  
        $assessment = $_SESSION['id'] . $sessionConcat;
        include('connect.php');
    
        $query = "SELECT q.SessionId, s.SessionName, q.QuestionId, q.QuestionContent, 
          GROUP_CONCAT(DISTINCT Answer ORDER BY Answer SEPARATOR '') AS Answer, 
        q.QuestionMarks 
        FROM Session s 
        INNER JOIN Question q ON s.SessionId = q.SessionId
        JOIN Answer an ON q.QuestionId = an.QuestionId AND an.SessionId = q.SessionId
        WHERE s.SessionName = ?
        GROUP BY an.SessionId, an.QuestionId
        ORDER BY q.QuestionId, an.Answer
        ";
    
        // prepare query
        $stmt=$mysqli->prepare($query);
        // You only need to call bind_param once
        $stmt->bind_param("s", $assessment);
        // execute query
        $stmt->execute(); 
    
        // This will hold the search results
        $searchQuestionId = array();
        $searchQuestionContent = array();
        $searchAnswer = array();
    
        // Fetch the results into an array
        // get result and assign variables (prefix with db)
        $stmt->bind_result($dbQuestionId, $dbQuestionContent, $dbAnswer);
        while ($stmt->fetch()) {
        $searchQuestionId[] = $dbQuestionId;
        $searchQuestionContent[] = $dbQuestionContent;
        $searchAnswer[] = $dbAnswer;
        }   
        ?>      
        </head>
        <body>
        <form id="QandA" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
        <?php 
    echo "<table border='1' id='markstbl'>
          <tr>
          <th class='questionth'>Question No.</th>
          <th class='questionth'>Question</th>
          <th class='answerth'>Answer</th>
          <th class='answermarksth'>Marks per Answer</th>
          <th class='noofmarksth'>Total Marks</th>
          </tr>\n";
          $previous_question_id = null;
          foreach ($searchQuestionContent as $key=>$question) {
            if ($previous_question_id == $searchQuestionId[$key]) {
          $searchQuestionId[$key] = '';
          $question = '';
      }else{
          $previous_question_id = $searchQuestionId[$key];
      }
            echo '<tr class="questiontd">'.PHP_EOL;
            echo '<td class="optiontypetd">'.htmlspecialchars($searchQuestionId[$key]).'</td>' . PHP_EOL;
            echo '<td>'.htmlspecialchars($question).'</td>' . PHP_EOL;
            echo '<td class="answertd">';
            echo $searchAnswer[$key];
            echo '</td>' ;
            echo '<td class="answermarkstd"><input class="individualMarks" name="answerMarks[]" id="individualtext" type="text" "/></td>' . PHP_EOL;
            echo '<td class="noofmarkstd">'.htmlspecialchars($searchMarks[$key]).'</td>' . PHP_EOL;
    }
            echo "</table>" . PHP_EOL;
    
        ?>
        </form>
        </body>
    

    以下是会话,问题和答案表的内容:

    会议桌:

    SessionId (auto)  SessionName
    1                 AAA
    2                 AAB
    

    问题表:

    SessionId  QuestionId (auto)  QuestionContent
    1          1                  What is 2+2?
    1          2                  Name the 3 hobbits?
    

    答案表:

    AnswerId (auto) SessionId  QuestionId   Answer
    1               1          1           B   
    2               1          2           B
    3               1          2           C
    4               1          2           E
    

1 个答案:

答案 0 :(得分:0)

GROUP_CONCAT(DISTINCT Answer ORDER BY Answer SEPARATOR '') AS Answer更改为Answer并尝试在foreach循环中进行此更改。

$previous_question_id = null;
foreach ($searchQuestionContent as $key=>$question) {
  if ($previous_question_id == $searchQuestionId[$key]) {
    $searchQuestionId[$key] = '';
    $question = '';
  } else {
    $previous_question_id = $searchQuestionId[$key];
  }