2014-10-01 73 views
2

我要展示一些從我的數據庫稱爲隨機問題,並使用代碼檢查答案隨機多項選擇題:檢查答案從數據庫

question.php:

<html> 
<body> 
<h3>Please answer all questions.</h3><br><br> 
<form action="phpinfo.php" method="post"> 
<?php 
//connect to db 
$server = mysql_connect ('localhost', 'username', 'password'); 
mysql_select_db("mc", $server); 
$question = mysql_query("SELECT * FROM `question` ORDER BY RAND() LIMIT 20;"); // 60 questions in my database 
$x = 0; 
while ($row = mysql_fetch_array($question)) 
{ 
    echo "Question: ".$row['q_do'] . '<br />'; //q_do are my questions 
    echo '<input type="radio" name="a'.$x.'" value=a />' ,"A ".$row['a'] . '<br />'; // answer a 
    echo '<input type="radio" name="a'.$x.'" value=b />' ,"B ".$row['b'] . '<br />'; // answer b 
    echo '<input type="radio" name="a'.$x.'" value=c />' ,"C ".$row['c'] . '<br />'; // answer c 
    echo '<input type="radio" name="a'.$x.'" value=d />' ,"D ".$row['d'] . '<br />'; // answer d 
    $x++; 
    }; 
mysql_close($server); 
?> 
<input type="submit" name="Submit" value="Submit" /> //go to next page 
<br> 
</form> 
</body> 
</html> 

phpinfo.php的:

<html> 
<body> 
<?php 
//connect to db 
$server = mysql_connect ('localhost', 'username', 'password'); 
mysql_select_db("mc", $server); 
$question = mysql_query("SELECT * FROM `question` ;"); 
$x = 0; 
$score = 0; 
while ($row = mysql_fetch_array($question)) //do not know how to call the questions did in previous page since they are random 
{ 
    echo $row['q_do'] . '<br />'; 

    $answered = $row[$_POST['a'.$x]] ; // the answers 
    $correct = $row['corr_ans'] ; 

    if ($answered == $correct) { 
     $score++; 
     $acolor = 'green' ; 
    } 
    else { 
    $acolor = 'red' ; 
    } 
    echo 'you answered <font color=' . $acolor . '>' . $answered . '<font color=black> <br />'; 
    echo 'the correct answer was ' . $correct . '<br />' ; 
    echo '-------------------------------------- <br />' ; 
    $x++; 
} 
echo 'You had a total of ' . $score . ' out of ' . $x . ' questions right!'; 
mysql_close($server); 
?> 
</body> 
</html> 

如何更改上述代碼以使整個系統正常工作? 這些代碼適用於沒有隨機問題並且問題依次爲升序的情況。

+1

將問題編號置於隱藏輸入中。然後表單處理腳本可以查找正確的問題。 – Barmar 2014-10-01 06:35:37

回答

0

爲您解決問題最簡單的方法是簡單地將問題的ID附加到每個問題的這樣的價值屬性:

echo '<input type="radio" name="a'.$x.'" value="a'.$row['id'].'" />' ,"A ".$row['a'] . '<br />'; 

這會給你的每一個問題的唯一答案值,你可以用來獲取問題的ID,然後從數據庫中提取問題。

因此,如果您的答案之一的值爲'b7',您知道用戶用選項b回答了問題7。

從字符串獲取ID,如「B7」,你可以使用以下命令:

$questionId = filter_var($fullAnswer, FILTER_SANITIZE_NUMBER_INT); 

因爲現在你知道問題是什麼,你可以使用id從數據庫中獲取它,做的該問題的其餘處理。

所以在你的服務器端腳本,你將不得不改變你的循環迭代20次(因爲那是多少個問題已經提交)。這是你的這兩個文件應該怎麼照顧你做出所有必要的改變:

question.php:

<html> 
<body> 
<h3>Please answer all questions.</h3><br><br> 
<form action="phpinfo.php" method="post"> 
<?php 
//connect to db 
$server = mysql_connect ('localhost', 'username', 'password'); 
mysql_select_db("mc", $server); 
$question = mysql_query("SELECT * FROM `question` ORDER BY RAND() LIMIT 20;"); // 60 questions in my database 
$x = 0; 
while ($row = mysql_fetch_array($question)) 
{ 
    echo "Question: ".$row['q_do'] . '<br />'; //q_do are my questions 
    echo '<input type="radio" name="a'.$x.'" value="a'.$row['id'].'" />' ,"A ".$row['a'] . '<br />'; 
    echo '<input type="radio" name="a'.$x.'" value="b'.$row['id'].'" />' ,"B ".$row['b'] . '<br />'; 
    echo '<input type="radio" name="a'.$x.'" value="c'.$row['id'].'" />' ,"C ".$row['c'] . '<br />'; 
    echo '<input type="radio" name="a'.$x.'" value="d'.$row['id'].'" />' ,"D ".$row['d'] . '<br />'; 
    $x++; 
    }; 
mysql_close($server); 
?> 
<input type="submit" name="Submit" value="Submit" /> //go to next page 
<br> 
</form> 
</body> 
</html> 

phpinfo.php的:

<html> 
<body> 
<?php 
//connect to db 
$server = mysql_connect ('localhost', 'username', 'password'); 
mysql_select_db("mc", $server); 
$x = 0; 
$score = 0; 

// Make 20 iterations to process the 20 answers 
for ($i=0; $i<=19; $i++) 
{ 
    $fullAnswer = $row[$_POST['a'.$x]] ; 

    // Find the question id 
    $questionId = filter_var($fullAnswer, FILTER_SANITIZE_NUMBER_INT); 

    // Find the answer 
    $answer = substr($fullAnswer, 0, 1); 

    // get the question for the answer of current iteration 
    $result = mysql_query("SELECT * FROM `question` WHERE id=$questionId LIMIT 1;"); 
    $question = mysql_fetch_assoc($result); 

    echo $question['q_do'] . '<br />'; 

    $correct = $question['corr_ans'] ; 

    if ($answer == $correct) { 
     $score++; 
     $acolor = 'green' ; 
    } 
    else { 
    $acolor = 'red' ; 
    } 
    echo 'you answered <font color=' . $acolor . '>' . $answer . '<font color=black> <br />'; 
    echo 'the correct answer was ' . $correct . '<br />' ; 
    echo '-------------------------------------- <br />' ; 
    $x++; 
} 
echo 'You had a total of ' . $score . ' out of ' . $x . ' questions right!'; 
mysql_close($server); 
?> 
</body> 
</html> 

簡單:)

這可以工作,但我沒有測試。

只是爲了讓你知道,使用mysql_query是一個壞主意。它被折舊。您應該使用PDO。查找如何使用它的PHP文檔,然後更改所有數據庫內容以在代碼中使用它。

祝你好運。

+1

你也可以使用[filter_var](http://php.net/filter_var):https://eval.in/200587 – 2014-10-01 07:41:48

+0

我已經改變了正則表達式。它更簡單,然後我猜是正則表達式。感謝您的建議:) – 2014-10-01 07:46:42

+1

也許for循環是:for($ i = 0; $ i <= 19; $ i ++)? – 2014-10-01 09:30:39