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 宇视监控服务器无法登录
  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)