A little help on my random quiz pleaseee!!

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
anoni_mouse
Forum Newbie
Posts: 1
Joined: Mon Jul 17, 2006 4:52 am

A little help on my random quiz pleaseee!!

Post 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]
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply