Random order of set fields

3 posts by 2 authors in: Forums > CMS Builder
Last Post: April 17, 2009   (RSS)

By rjbathgate - April 16, 2009 - edited: April 16, 2009

Hi,

I figure this'll be slightly more complex than a simple RAND...

I have this:
<select>
<option><?php echo $record['wrong_answer_1'] ?></option>
<option><?php echo $record['wrong_answer_2'] ?></option>
<option><?php echo $record['wrong_answer_3'] ?></option>
<option><?php echo $record['correct_answer'] ?></option>
</select>


So a drop down, showing 4 fields from a CMS table.

I want to have the order of these 4 <options> to be random -- basically otherwise, the correct answer will always be in the same place!0

It's not a FOREACH as it's fixed fields of a record, so figure a simple RAND order won't work.

I'm thinking something along lines of;

ARRAY:
Wrong answer 1
Wrong answer 2
Wrong answer 3
Correct answer

DISPLAY
Choose any one randomly from the array and display <option>xxxx</option>

Now choose any other one randomly from the array, but ensure it's not one already displayed.

Is this easy/possible?!

Cheers

Re: [ross] Random order of set fields

Hey,

Got it going with this:

<?php
$answers = array
(
$record['wrong_answer_1'],
$record['wrong_answer_2'],
$record['wrong_answer_3'],
$record['correct_answer']
);
$possible = shuffle( $answers );
print( "<select>\n" );
foreach ( $answers as $possible )
{
print( "<option value=\"".$possible."\">$possible</option>\n" );
}
print( "</select>\n" );
?>


:) Cheers Ross