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:
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
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: [rjbathgate] Random order of set fields
By ross - April 17, 2009
Hi there
Thanks for the post!
I think you are on the right track here. What I would do is create an array and load each value from your record into it one by one.
Then, you can randomize that array and use a foreach loop to create a <select> menu with it.
Does that make sense? Let me know where you get to with the code and we'll go from there.
Thanks!
Thanks for the post!
I think you are on the right track here. What I would do is create an array and load each value from your record into it one by one.
Then, you can randomize that array and use a foreach loop to create a <select> menu with it.
Does that make sense? Let me know where you get to with the code and we'll go from there.
Thanks!
-----------------------------------------------------------
Cheers,
Ross Fairbairn - Consulting
consulting@interactivetools.com
Hire me! Save time by getting our experts to help with your project.
Template changes, advanced features, full integration, whatever you
need. Whether you need one hour or fifty, get it done fast with
Priority Consulting: http://www.interactivetools.com/consulting/
Cheers,
Ross Fairbairn - Consulting
consulting@interactivetools.com
Hire me! Save time by getting our experts to help with your project.
Template changes, advanced features, full integration, whatever you
need. Whether you need one hour or fifty, get it done fast with
Priority Consulting: http://www.interactivetools.com/consulting/
Re: [ross] Random order of set fields
By rjbathgate - April 17, 2009
Hey,
Got it going with this:
:) Cheers Ross
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