【问题标题】:Want to get the data in the html table displayed correctly想让html表格中的数据正确显示
【发布时间】:2012-11-05 01:12:13
【问题描述】:

我对如何在我的 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. 我希望文本输入也显示在自己的行中,就像答案一样:

我的问题是如何实现第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

【问题讨论】:

    标签: php html sql mysqli


    【解决方案1】:

    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];
      }
    

    【讨论】:

    • 我已经尝试过您给我的回答,但我担心它没有奏效。在 HTML 表的“答案”列中,问题 1 的答案显示为 BB,而应仅为 B,而问题 2 的答案也显示为 BB,而应为 BCE .它会显示任一问题的一个答案,并在两个问题中显示它(如果有意义)
    • 我更新了有问题的表格,使其包含您的 foreach 循环
    • echo $searchAnswer[$key];替换foreach($searchAnswer as $key=&gt;$answer){ echo "$answer&lt;br /&gt;"; }
    • 好吧,现在它正在做的只是为每个问题显示一个答案,显然问题一很好,因为它只是一个答案,因为它显示答案 B,对于问题 2它有多个答案,但它只显示多个答案中的第一个,也是B。代码更新
    • 从查询中的GROUP BY 子句中删除an.QuestionId
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-12
    • 2019-12-09
    • 1970-01-01
    • 2012-10-30
    • 1970-01-01
    • 1970-01-01
    • 2019-10-14
    相关资源
    最近更新 更多