Page 1 of 1

A little help on my random quiz pleaseee!!

Posted: Mon Jul 17, 2006 5:10 am
by anoni_mouse
Pimptastic | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Anyone knows how to make this random quiz worked without the same thing came out twice or more?

I'm confused, coz i dont really know bout php programming, only a little... 

This is the code that i have:

Code: Select all

<?
	include("dbConnect.php");

	$arrID[10];
	$val = "false";
	$k = 1;
	
	for($i=1;$i<=10;$i++){
		$arrID[$i] = 0;
	}
	
	for($i=1; $i<=10; $i++)
	{
			$hslRandom = rand(1,15);
			$arrID[$k] = $hslRandom;
			$k=$k+1;
			$val = "true";
	
	
		$sql = "select * from pertanyaan where id_pertanyaan = " . $hslRandom;
		$result = mysql_query($sql);
		$content = mysql_fetch_array($result);
		echo " ". $i. ".". " ". $content["pertanyaan"]."<br><br>";
	}
?>
A little help please??!! I appreciate it, thanx!!


Pimptastic | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Mon Jul 17, 2006 10:04 am
by pickle
In your second for loop, put the random number generation in a do...while loop. In there, you can check if the random number you've generated, has happened yet:

Code: Select all

for($i=1; $i<=10; $i++)
{
   do
   {
       $hslRandom = rand(1,15);
   }
   while(!in_array($hlsRandom,$arrID))

   $arrID[$k] = $hlsRandom;
   ...
}
If you're looking to randomly choose 10/15 questions though, you may be in that loop for a little while.