douyuqing_12345 2016-02-26 08:08
浏览 28
已采纳

单行数据库的随机输出

I have an exam application web-based, which has the question, 5 selection answers, and the correct answer. The selection answers is using radio, but I want to make the selection answer output is random. There is simply the table :

tb_question:
    id_question: int(8) AUTO_INCREMENT PRIMARY_KEY
    question: varchar(1000)
    option_a: varchar(512)
    option_b: varchar(512)
    option_c: varchar(512)
    option_d: varchar(512)
    option_e: varchar(512)
    answer: enum('A', 'B', 'C', 'D', 'E')

And this is my html code :

<ol>
    <?php foreach($select_question as $row): ?>
        <li>
            <?php echo $row->question; ?><br />
            <input type="radio" name="<?php echo $row->id_question; ?>" value="A"> <?php echo $row->option_a; ?> <br />
            <input type="radio" name="<?php echo $row->id_question; ?>" value="B"> <?php echo $row->option_b; ?> <br />
            <input type="radio" name="<?php echo $row->id_question; ?>" value="C"> <?php echo $row->option_c; ?> <br />
            <input type="radio" name="<?php echo $row->id_question; ?>" value="D"> <?php echo $row->option_d; ?> <br />
            <input type="radio" name="<?php echo $row->id_question; ?>" value="E"> <?php echo $row->option_e; ?> <br />
        </li>
    <?php endforeach; ?>
</ol>

And I want the radio options will be shown mixed, not ordered like above. Can someone help me? Thanks !

  • 写回答

1条回答 默认 最新

  • doucai6663 2016-02-26 08:18
    关注

    You can get all answers in an array and then you can use shuffle() function of PHP to suffle them: like: $answersShuffled=shuffle($answers);

    Now you can iterate through $answersShuffled array.

    But I would suggest to normalize your database and keeping answers in seprate table. There could be a case when your number of options can be increased

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来