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

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行