douanye8442 2017-03-05 19:40
浏览 20
已采纳

在PHP中使用多维数组创建测验

I'm new to PHP and I need to create a quiz using a multidimensional associative array. The quiz prints out like it is supposed to but I am having two issues, one is that I am having trouble trying to make the radio buttons sticky. The other issue is that I want the key "3" to be the answer to all of the questions and I can't seem to figure out a way to count how many times "3" is checked and print out the answer. I've tried a lot of different things over the past 7 hours but nothing seems to work they way I want it to. Do you have any tips or suggestions?

$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/>";
}
}
?>
  • 写回答

1条回答 默认 最新

  • douji9816 2017-03-05 19:44
    关注

    Your modified code:

    $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>";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵