2012-10-11 29 views
1

我有一個「答案」數據庫表如下:這是一個PHP/HTML表格行顯示不正確的答案

回答表現在

AnswerId SessionId QuestionId Answer 
13  AAA  1   A 
14  AAB2  2   A 
15  AAB2  2   B 

,你可以看到有1名回答問題1在考試(Session)AAA中,這些是考試(Session)AAB2中問題2的2個答案。

下面是「問題」表:

問表:

SessionId QuestionId QuestionContent NoofAnswers ReplyId QuestionMarks OptionId 
AAA  1   What is 2+2? 1   1  5    2 
AAC  1   3+3 and 4+4 ? 2   2  10   6 

現在我已經在用戶中的一個術語,從一個問題進入並編譯檢索的檢索功能。因此,舉例來說,如果用戶在「+」進入,然後下面是什麼結果應該在PHP/HTML表格顯示:

QuestionContent Option Type Number of Answers Answer Number of Replies Number of Marks 
    What is 2+2? A-D   1     A  Single    5 
    3+3 and 4+4? A-H   2     AB  Multiple    5 

但問題是,它顯示的是一個額外的答案,它會顯示這個如下:

QuestionContent Option Type Number of Answers Answer Number of Replies Number of Marks 
    What is 2+2? A-D   1     A AB  Single    5 
    3+3 and 4+4? A-H   2     A AB  Multiple    5 

現在我的問題是,爲什麼它在兩行顯示錯誤的答案?第一行應該只是「A」,第二行應該只是「AB」。

下面是它執行查詢,並將結果輸出的代碼(我已經降低,從而更容易讓你閱讀,希望看到的問題):

<?php 

//connect to db 


    // Build the query 
    $questionquery = " 
SELECT DISTINCT q.QuestionContent, o.OptionType, q.NoofAnswers, GROUP_CONCAT(DISTINCT Answer SEPARATOR '') AS Answer, r.ReplyType, 
     q.QuestionMarks, q.SessionId 
    FROM Answer an 
    INNER JOIN Question q ON q.QuestionId = an.QuestionId and an.SessionId = q.SessionId 
    JOIN Reply r ON q.ReplyId = r.ReplyId 
    JOIN Option_Table o ON q.OptionId = o.OptionId 
     WHERE ".implode(" AND ", array_fill(0, $numTerms, "q.QuestionContent LIKE ?"))." 
     GROUP BY an.SessionId, an.QuestionId 
     ORDER BY ".implode(", ", array_fill(0, $numTerms, "IF(q.QuestionContent LIKE ?, 1, 0) DESC"))." 
    "; 

    // Make the referenced array 
    $referencedArray = make_values_referenced(array_merge(
     array(str_repeat("ss", $numTerms)), // types 
     $termArray,       // where 
     $termArray       // order by 
    )); 


    // Bind parameters 
    if (!call_user_func_array(array($stmt, 'bind_param'), make_values_referenced($referencedArray))) { 
     die("Error binding parameters: $stmt->error"); 
    } 


    // This will hold the search results 
    $searchResults = array(); 
    $searchOption = array(); 
    $searchNoofAnswers = array(); 
    $searchAnswer = array(); 
    $searchReply = array(); 
    $searchMarks = array(); 

    // Fetch the results into an array 
    if (!$stmt->num_rows()) { 
     $stmt->bind_result($dbQuestionContent,$dbOptionType,$dbNoofAnswers,$dbAnswer,$dbReplyType,$dbQuestionMarks, $dbSessionId); 
     while ($stmt->fetch()) { 
     $searchResults[] = $dbQuestionContent; 
     $searchOption[] = $dbOptionType; 
     $searchNoofAnswers[] = $dbNoofAnswers; 
     $searchAnswer[] = $dbAnswer; 
     $searchReply[] = $dbReplyType; 
     $searchMarks[] = $dbQuestionMarks; 
     } 
    } 

    } 


     $questionnum = sizeof($searchResults); 

    // If $searchResults is not empty we got results 
    if (!empty($searchResults)) { 
     echo "<p>Your Search: '$inputValue'</p>"; 
     echo"<p>Number of Questions Shown from the Search: <strong>$questionnum</strong></p>"; 
     echo "<table border='1' id='resulttbl'> 
     <tr> 
     <th class='questionth'>Question</th> 
     <th class='optiontypeth'>Option Type</th> 
     <th class='noofanswersth'>Number of <br/> Answers</th> 
     <th class='answerth'>Answer</th> 
     <th class='noofrepliesth'>Number of <br/> Replies</th> 
     <th class='noofmarksth'>Number of <br/> Marks</th> 
     </tr>\n"; 
     $script = ''; 
     foreach ($searchResults as $key=>$question) { 
     $script .= 'var key_' . $key . '="' . str_replace('"','\"', $question) . '";' . PHP_EOL; 
     echo '<tr class="questiontd">'.PHP_EOL; 
     echo '<td>'.htmlspecialchars($question).'</td>' . PHP_EOL; 
     echo '<td class="optiontypetd">'.htmlspecialchars($searchOption[$key]).'</td>' . PHP_EOL; 
     echo '<td class="noofanswerstd">'.htmlspecialchars($searchNoofAnswers[$key]).'</td>' . PHP_EOL; 
     echo '<td class="answertd">'.htmlspecialchars(implode(' ', $searchAnswer)).'</td>' . PHP_EOL; 
     echo '<td class="noofrepliestd">'.htmlspecialchars($searchReply[$key]).'</td>' . PHP_EOL; 
     echo '<td class="noofmarkstd">'.htmlspecialchars($searchMarks[$key]).'</td>' . PHP_EOL; 
     echo "<td class='addtd'><button type='button' class='add' onclick=\"parent.addwindow(key_$key,'$searchMarks[$key]','$searchNoofAnswers[$key]','$searchOption[$key]','$searchReply[$key]','$searchAnswer[$key]');\">Add</button></td></tr>"; 
} 
     echo "</table>" . PHP_EOL; 
     echo '<script type="text/javascript">' . PHP_EOL; 
     echo $script; 
     echo '</script>' . PHP_EOL; 


} 
?> 

回答

0

你檢查一下查詢本身返回?因此,您將確定問題是在查詢中還是在結果操作中。

當您獲取結果時,您創建了一個追加行的$searchAnswer數組。然後在表格中打印整個$searchAnswer數組(可能會導致它崩潰)。這不是問題嗎?

+0

是的,你是對的,我不需要內爆$ searchAnswer。只需輸出$ searchAnswer [$ key]數組。謝謝 – user1701484

相關問題