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 !

2
  • How to randomly sort list items? Commented Feb 26, 2016 at 8:17
  • 1
    You must normalize your database and to keep answers into separate table. Commented Feb 26, 2016 at 8:17

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

2 Comments

which the things that I need to make it into array? Is the $row->option_a until $row->option_e ?
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.