Reputation: 513
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 !
Upvotes: 0
Views: 49
Reputation: 158
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
Upvotes: 1