Jump to content

Recommended Posts

I'm looking for a bit of advice regards the code shown below. I am implementing a questionairre in html/php and got some great help on the code in another thread. One thing i would like to be able to do with my answers is to have them randomly display on the screen instead of them being in the same order all the time. For example, one question may be: What make of mobile phone do you own? A)Nokia B)Motorolla C)Siemens D)Sony. The choices will always be the same except i would like Nokia to be in a different positions from choice A the next time someone runs the questionairre. Is this possible?


if($successful) {
echo '<table align="left">';
while ($row=mysql_fetch_array($result,MYSQL_NUM)){

$id = $row[0];
$q = $row[1];
$answer1 = $row[2];
$answer2 = $row[3];
$answer3 = $row[4];
$answer3 = $row[4];

echo'<tr><td align=\"left\">'.$q.'</td></tr>
<tr><td align=\"left\"><input type="radio" name="option['.$id.']"value="'.$answer1.'"/>'.$answer1.'</td></tr>
<tr><td align=\"left\"><input type="radio" name="option['.$id.']"value="'.$answer2.'"/>'.$answer2.'</td></tr>
<tr><td align=\"left\"><input type="radio" name="option['.$id.']"value="'.$answer3.'"/>'.$answer3.'</td></tr>
<tr><td align=\"left\"><input type="radio" name="option['.$id.']"value="'.$answer4.'"/>'.$answer4.'</td></tr>'."\n";
}
echo '</table>';
echo '</form>';

}else{
echo'<p><b>Error. Please contact admin</b></p>';
}
Link to comment
https://forums.phpfreaks.com/topic/4707-randomising-choices/
Share on other sites

this is not the full answer, you you should get the picture pretty easily...
You could even randomize the orders directly instead...

[code]
$rand=rand(1,4)
if($successful)    {
echo '<table align="left">';
while ($row=mysql_fetch_array($result,MYSQL_NUM)){

$id = $row[0];
$q = $row[1];

if($rand==1){
$answer1 = $row[2];
$answer3 = $row[3];
$answer2 = $row[4];
$answer4 = $row[5];}

elseif($rand==2){
$answer2 = $row[2];
$answer4 = $row[3];
$answer3 = $row[4];
$answer1 = $row[5];}

elseif($rand==3){
$answer4 = $row[2];
$answer1 = $row[3];
$answer3 = $row[4];
$answer2 = $row[5];}

else($rand==4){
$answer3 = $row[2];
$answer4 = $row[3];
$answer1 = $row[4];
$answer2 = $row[5];}

echo'<tr><td align=\"left\">'.$q.'</td></tr>
<tr><td align=\"left\"><input type="radio" name="option['.$id.']"value="'.$answer1.'"/>'.$answer1.'</td></tr>
<tr><td align=\"left\"><input type="radio" name="option['.$id.']"value="'.$answer2.'"/>'.$answer2.'</td></tr>
<tr><td align=\"left\"><input type="radio" name="option['.$id.']"value="'.$answer3.'"/>'.$answer3.'</td></tr>
<tr><td align=\"left\"><input type="radio" name="option['.$id.']"value="'.$answer4.'"/>'.$answer4.'</td></tr>'."\n";
}
echo '</table>';
echo '</form>';

}else{
echo'<p><b>Error. Please contact admin</b></p>';
}
[/code]
Link to comment
https://forums.phpfreaks.com/topic/4707-randomising-choices/#findComment-16493
Share on other sites

or try

[code]if($successful) {
echo '<table align="left">';
while ($row=mysql_fetch_array($result,MYSQL_NUM)){

$id = $row[0];
$q = $row[1];
   // put answers into array and shuffle them
$answer = array_slice($row, 2, 4);
shuffle($answer);

echo'<tr><td align=\"left\">'.$q.'</td></tr>
<tr><td align=\"left\"><input type="radio" name="option['.$id.']"value="'.$answer[0].'"/>'.$answer[0].'</td></tr>
<tr><td align=\"left\"><input type="radio" name="option['.$id.']"value="'.$answer[1].'"/>'.$answer[1].'</td></tr>
<tr><td align=\"left\"><input type="radio" name="option['.$id.']"value="'.$answer[2].'"/>'.$answer[2].'</td></tr>
<tr><td align=\"left\"><input type="radio" name="option['.$id.']"value="'.$answer[3].'"/>'.$answer[3].'</td></tr>'."\n";
}
echo '</table>';
echo '</form>';

}else{
echo'<p><b>Error. Please contact admin</b></p>';
}[/code]
Link to comment
https://forums.phpfreaks.com/topic/4707-randomising-choices/#findComment-16533
Share on other sites

[!--quoteo(post=354000:date=Mar 11 2006, 08:36 PM:name=AV1611)--][div class=\'quotetop\']QUOTE(AV1611 @ Mar 11 2006, 08:36 PM) [snapback]354000[/snapback][/div][div class=\'quotemain\'][!--quotec--]
this is not the full answer, you you should get the picture pretty easily...
You could even randomize the orders directly instead...

[code]
$rand=rand(1,4)
if($successful)    {
echo '<table align="left">';
while ($row=mysql_fetch_array($result,MYSQL_NUM)){

$id = $row[0];
$q = $row[1];

if($rand==1){
$answer1 = $row[2];
$answer3 = $row[3];
$answer2 = $row[4];
$answer4 = $row[5];}

elseif($rand==2){
$answer2 = $row[2];
$answer4 = $row[3];
$answer3 = $row[4];
$answer1 = $row[5];}

elseif($rand==3){
$answer4 = $row[2];
$answer1 = $row[3];
$answer3 = $row[4];
$answer2 = $row[5];}

else($rand==4){
$answer3 = $row[2];
$answer4 = $row[3];
$answer1 = $row[4];
$answer2 = $row[5];}

echo'<tr><td align=\"left\">'.$q.'</td></tr>
<tr><td align=\"left\"><input type="radio" name="option['.$id.']"value="'.$answer1.'"/>'.$answer1.'</td></tr>
<tr><td align=\"left\"><input type="radio" name="option['.$id.']"value="'.$answer2.'"/>'.$answer2.'</td></tr>
<tr><td align=\"left\"><input type="radio" name="option['.$id.']"value="'.$answer3.'"/>'.$answer3.'</td></tr>
<tr><td align=\"left\"><input type="radio" name="option['.$id.']"value="'.$answer4.'"/>'.$answer4.'</td></tr>'."\n";
}
echo '</table>';
echo '</form>';

}else{
echo'<p><b>Error. Please contact admin</b></p>';
}
[/code]
[/quote]

I tried this and it worked really well. Many thanks. Thanks also to the other reply, i didn't try it but thnks for the effort.
Link to comment
https://forums.phpfreaks.com/topic/4707-randomising-choices/#findComment-16672
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.