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

报告相同问题?

悬赏问题

  • ¥100 将USDZ文件转化为带颜色的OBJ文件
  • ¥15 对象代号: , 表单: 不存在!
  • ¥15 WebSocket的问题
  • ¥15 centos上启动kylin后网页报错404
  • ¥20 使用hackrf进行信号收发时接收到的信号幅度太小
  • ¥15 WebSocket的问题
  • ¥15 BDSBAS-B1C和B1C信号有什么不同
  • ¥15 在半圆平面内随机生成点坐标
  • ¥15 系统容量变化的几种多址方式TDMA, CDMA,FDMA,OFDMA 对比,应该给的是一个曲线 图,随着系统容量的增加,几种多址方式性能的对比 图,MATLAB程序仿真折线图
  • ¥15 用visual Studio 写c ++只运行上一个旧代码的运行结果是怎么回事