2013-01-22 55 views
0

我正在做一個測驗系統,並嘗試了一種從MySQL獲取變量和值的不同方式。單選按鈕和檢查用戶在測驗中的選擇

$question = mysql_query("SELECT * FROM `questions`"); 
$stat = mysql_fetch_assoc($question); 
$num = mysql_num_rows($question); 
$questionid = 0; 
for($i=0;$i<=$num;$i++) 
{ 
$question = mysql_query("SELECT * FROM `questions` WHERE `id`='$i'"); 
$stat = mysql_fetch_assoc($question); 
//if($stat['answer'] == null 
echo $stat['question'] . '<br />'; 
echo '<input type="radio" name="' . $i .'" value="' . $questionid . '" />' . $stat['answer1'] . '<br />'; 
echo '<input type="radio" name="$i" value"$questionid" />' . $stat['answer2'] . '<br />'; 
echo '<input type="radio" name="$i" value"$questionid" />' . $stat['answer3'] . '<br />'; 
echo '<input type="radio" name="$i" value"$questionid" />' . $stat['answer4'] . '<br />'; 
$questionid++; 
} 

現在,我想讓人選擇正確的答案,但我儘量選擇在問題1回答,然後在問題2,它不會讓我可能是因爲無線電具有相同的名稱 - 我不知道如何讓學生在每個問題中選擇一個答案,以及如何選擇(將其存儲在一個變量中,並檢查答案是否正確)。

回答

0

你必須用PHP變量和引號(問題 '/ 「)。串連運營商都沒有使用正確的

名=」。'。 $ i。'「value =」'。 $ questionid。 「」

 /\ /\  /\   /\ 

見代碼:

for($i=0;$i<=$num;$i++) 
{ 
$question = mysql_query("SELECT * FROM `questions` WHERE `id`='$i'"); 
$stat = mysql_fetch_assoc($question); 
//if($stat['answer'] == null 
echo $stat['question'] . '<br />'; 
echo '<input type="radio" name="' . $questionid .'" value="' . $stat['answer1'] . '" />' . $stat['answer1'] . '<br />'; 
echo '<input type="radio" name="' . $questionid .'" value="' . $stat['answer2'] . '" />' . $stat['answer2'] . '<br />'; 
echo '<input type="radio" name="' . $questionid .'" value="' . $stat['answer3'] . '" />' . $stat['answer3'] . '<br />'; 
echo '<input type="radio" name="' . $questionid .'" value="' . $stat['answer4'] . '" />' . $stat['answer4'] . '<br />'; 
$questionid++; 
} 
+0

如果每個按鈕的值是'$ questionid',那麼您將不知道按下了哪個按鈕...請參閱我的回答http://stackoverflow.com/questions/14462380/how-can -i-看到-IF-的用戶,選擇功能於the-測驗是正確的更完整的解決方案 – Floris

+0

@弗洛伊斯:我只想到報價。你是對的。每個單選按鈕應該有相同的問題ID名稱,但不同的值即回答。 –

0

我不認爲你這樣做的方式是good.I認爲重複代碼最好的方法是同時()loop.Like

$all = mysql_query("SELECT * FROM 'questions'"); 
while($all_array=mysql_fetch_array($all)) 
{ 
$question = mysql_query("SELECT * FROM questions WHERE id='".$all_array['id']."'"); 
while($stat=mysql_fetch_array(question)){ 
echo $stat['question'] . '<br />'; 
echo '<input type="radio" name="' .$stat['id'].'" value="' . $stat['answer1'] . '" />' . $stat['answer1'] . '<br />'; 
echo '<input type="radio" name="' .$stat['id'].'" value="' . $stat['answer2'] . '" />' . $stat['answer2'] . '<br />'; 
echo '<input type="radio" name="' .$stat['id'].'" value="' . $stat['answer3'] . '" />' . $stat['answer3'] . '<br />'; 
echo '<input type="radio" name="' .$stat['id'].'" value="' . $stat['answer4'] . '" />' . $stat['answer4'] . '<br />'; 
} 
}