0

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 !

Peyman Mohamadpour
  • 17,954
  • 24
  • 89
  • 100
imbagila
  • 513
  • 1
  • 8
  • 26

1 Answers1

1

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

Prashant
  • 158
  • 1
  • 2
  • 10
  • which the things that I need to make it into array? Is the `$row->option_a` until `$row->option_e` ? – imbagila Feb 26 '16 at 08:45
  • yes all options from $row->option_a to $row->option_e in array. Like `$answersShuffled[]=$row->option_a; $answersShuffled[]=$row->option_b; $answersShuffled[]=$row->option_c; $answersShuffled[]=$row->option_d; $answersShuffled[]=$row->option_e; ` and then shuffle arrray – Prashant Feb 26 '16 at 13:50