Array with rand function.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bumdeal2
Forum Newbie
Posts: 11
Joined: Thu Apr 01, 2010 11:12 am

Array with rand function.

Post 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>');

	}


?>
JakeJ
Forum Regular
Posts: 675
Joined: Thu Dec 10, 2009 6:27 pm

Re: Array with rand function.

Post 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.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Array with rand function.

Post 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>');
}
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
JakeJ
Forum Regular
Posts: 675
Joined: Thu Dec 10, 2009 6:27 pm

Re: Array with rand function.

Post by JakeJ »

Hey! That's easier! I didn't know about shuffle(). I'll have to remember that.
Post Reply