2017-03-05 69 views
0

我是PHP新手,需要使用多維關聯數組創建測驗。測驗打印出來應該是這樣,但我有兩個問題,一個是我在嘗試使單選按鈕粘滯時遇到問題。另一個問題是我想讓「3」鍵成爲所有問題的答案,我似乎無法找出一種方法來計算「3」被檢查的次數並打印出答案。過去7個小時我嘗試了很多不同的東西,但似乎沒有任何工作方式,我希望它。你有什麼提示或建議?在PHP中使用多維數組創建測驗

$quiz = array(
"What does HTML stand for?" => array(
    '1' => "Home Tool Markup Language", 
    '2' => "Hyperlinks and Text Markup Language", 
    '3' => "Hyper Text Markup Language", 
    '4' => "Hyper Text Manipulation Language", 
), 
"Choose the correct HTML tag for the smallest heading:" => array(
    '2' => "<heading>", 
    '1' => "<h1>", 
    '4' => "<head>", 
    '3' => "<h6>", 
), 
); 
foreach($quiz as $question => $answers) { 
    echo $question; 
    echo "<form>"; 
foreach($answers as $index => $answer) { 
    echo "<input type='radio' name=$option>".$answer."<br/>"; 
} 
} 
?> 

回答

0

您的修改後的代碼:

$quiz = array(
"What does HTML stand for?" => array(
    '1' => "Home Tool Markup Language", 
    '2' => "Hyperlinks and Text Markup Language", 
    '3' => "Hyper Text Markup Language", 
    '4' => "Hyper Text Manipulation Language", 
), 
"Choose the correct HTML tag for the smallest heading:" => array(
    '2' => "&lt;heading&gt;", 
    '1' => "&lt;h1&gt;", 
    '4' => "&lt;head&gt;", 
    '3' => "&lt;h6&gt;", 
), 
); 

echo "<form>"; 
foreach($quiz as $question => $answers) { 
    echo $question; 
    foreach($answers as $index => $answer) { 
     echo "<input type=\"radio\" name=\"$question\" value=\"$option\">$answer<br/>"; 
    } 
} 
echo "</form>"; 
+0

感謝米凱爾,是有很大幫助!你讓它變得粘稠!我認爲這會讓我更容易找出如何計算正確答案的數量。我很感激! – Taylor

+0

請在這種情況下答覆這個答案。 –