Page 1 of 1

Array with rand function.

Posted: Sun Apr 11, 2010 9:52 am
by bumdeal2
Hey guys im a bit stuck here, i was wondering how youd put the answer,ranswer,ranswer1,ranswer2 into an array, and use the rand function to switch the postions of the answer....so that there not in the same position.



Code: Select all

<?php

 

include('database_access_param.php');
include('auth.php');

$qid=$_POST['qid'];
$quesid=$_POST['quesid'];
$totalCorrect=$_POST['totalCorrect'];
$score=$_POST['score'];
$taken=$_POST['taken'];
import_request_variables("pgc","");


if(isset($_GET['quesid']))
 {

   $quesid=$_GET['quesid'];
}


if(isset($_GET['qid']))
{

   $qid=$_GET['qid'];

}
if(isset($_GET['totalCorrect']))
{

   $totalCorrect=$_GET['totalCorrect'];

}
if(isset($_GET['score']))
{

   $score=$_GET['score'];

}
if(isset($_GET['taken']))
{

   $taken=$_GET['taken'];

}

    mysql_connect( $hostname, $dbuser, $dbpassword)
        or die ( 'Unable to connect to server' );

    

    mysql_select_db($dbname )
        or die ( 'Unable to select database' );


    $sql = "SELECT * FROM questiona WHERE qid = '$qid' && quesid = '$quesid' ";
 



		print("</head>");
		print('<BODY onLoad="goNewWin()">');
		print('<form action="grad.php?qid=$qid&quesid=$quesid&totalCorrect=$totalCorrect&taken=$taken" method="post" id="quiz">');
		print('<table class="report" align=left width=100%>');
		
		print('<tr><td>&nbsp;</td></tr>');


		$result=mysql_query($sql) or die ('Unable execute the query');
;



		if(mysql_numrows($result))
		{
				$row = mysql_fetch_row($result);
				{

				$questid=$row[0];
				$quesid=$row[1];
				$question=$row[2];
				$ranswer=$row[3];
				$ranswer1=$row[4];
				$ranswer2=$row[5];
				$answer=$row[6];
				$qid=$row[7];


print('<body>');

				

	print('<tr><td >'.$quesid.'&nbsp;&nbsp;&nbsp;'.$question.'</td>'.$Quiz_Topic.'</tr>');

	print('<input type= hidden name=qid value="'.$qid.'">');

	print('<input type= hidden name=totalCorrect value="'.$totalCorrect.'">');

	print('<input type= hidden name=quesid value="'.$quesid.'">');

	print('<input type= hidden name=score value="'.$score.'">');

	print('<input type= hidden name=taken value="'.$taken.'">');
      
	print('<tr><td><input type="radio" name="answer" id="answer" value="'.$ranswer .'">&nbsp;&nbsp;&nbsp;'.$ranswer.'</td></tr>');

	print('<tr><td><input type="radio" name="answer" id="answer" value="'.$ranswer1 .'">&nbsp;&nbsp;&nbsp;'.$ranswer1 .'</td></tr>');

	print('<tr><td><input type="radio" name="answer" id="answer" value="'.$ranswer2 .'">&nbsp;&nbsp;&nbsp;'.$ranswer2 .'</td></tr>');

	print('<tr><td><input type="radio" name="answer" id="answer" value="answer">&nbsp;&nbsp;&nbsp;'.$answer .'</td></tr>');

	print('<tr><td>&nbsp;<input type=hidden name=rightanswer value="'.$answer.'"><input type= hidden name=qnumber value="'.$question.'"></tr>');


	}
	print('<tr><td colspan=2><input type=submit name=submit value="Submit" ><input type=reset name=reset value="Clear" ></td></tr></table>');
	
	 
	print('</form>');

	}
else
	{

	 echo "<div id='results'>Your result is:  $score%</div>";
	print('<input type =hidden name=score value="'.$score.'">');
	print('</form>');

	}


?>

Re: Array with rand function.

Posted: Mon Apr 12, 2010 1:31 pm
by JakeJ
You could try adding a 2nd element to your array using the random number function and then sorting on that element. Actually, you have to add the number as the 1st element in order to sort properly. That ought to do the trick for you.

Re: Array with rand function.

Posted: Mon Apr 12, 2010 1:44 pm
by AbraCadaver

Code: Select all

$answers = array($row[3], $row[4], $row[5], $row[6]);
shuffle($answers);

foreach($answers as $answer) {
    print('<tr><td><input type="radio" name="answer" id="answer" value="answer">&nbsp;&nbsp;&nbsp;' . $answer . '</td></tr>');
}

Re: Array with rand function.

Posted: Mon Apr 12, 2010 5:55 pm
by JakeJ
Hey! That's easier! I didn't know about shuffle(). I'll have to remember that.