Array with rand function.
Posted: Sun Apr 11, 2010 9:52 am
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> </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.' '.$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 .'"> '.$ranswer.'</td></tr>');
print('<tr><td><input type="radio" name="answer" id="answer" value="'.$ranswer1 .'"> '.$ranswer1 .'</td></tr>');
print('<tr><td><input type="radio" name="answer" id="answer" value="'.$ranswer2 .'"> '.$ranswer2 .'</td></tr>');
print('<tr><td><input type="radio" name="answer" id="answer" value="answer"> '.$answer .'</td></tr>');
print('<tr><td> <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>');
}
?>