Jump to content


Photo

randomising choices


  • Please log in to reply
3 replies to this topic

#1 MIR1999

MIR1999
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 11 March 2006 - 07:48 PM

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>';
}

#2 AV1611

AV1611
  • Members
  • PipPipPip
  • Advanced Member
  • 997 posts

Posted 11 March 2006 - 08:36 PM

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

$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>';
}


#3 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,016 posts

Posted 12 March 2006 - 12:35 AM

or try

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>';
}

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#4 MIR1999

MIR1999
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 12 March 2006 - 02:56 PM

[!--quoteo(post=354000:date=Mar 11 2006, 08:36 PM:name=AV1611)--][div class=\'quotetop\']QUOTE(AV1611 @ Mar 11 2006, 08:36 PM) View Post[/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...

$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>';
}
[/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.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users