6 Replies - 4633 Views - Last Post: 23 May 2008 - 04:33 PM Rate Topic: -----

#1 WishAngel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 22-May 08

Selecting random record from MySQL database table.

Post icon  Posted 22 May 2008 - 01:58 PM

Hello people...I`m new in php and i`ve in front off a "wall" to say it like this... I`ve created a quiz with admin side...you can introduce 20 questions ... all is allright...you can also create new quizzes but the questions for the new quiz is inserted in the same table from the database..but every quiz you made has a different "security number" and if you want to view one quiz the query look`s like:

$cerereSQL = "SELECT * FROM `intrebari` WHERE nrsecuritate='$nrsecuritate' ORDER BY id";
   $rezultat = mysql_query($cerereSQL);


nrsecuritate=security number...good...all fine till here..

What i want to do..and I don`t know how...is when a user want to take a quiz all the 20 questions from the database to be showed random...and they`re position to change everytime a user enters to take the quiz... If you could help me...you will make me a happy man :( >:D< Thanks in advance! Please help :(...

Is This A Good Question/Topic? 0
  • +

Replies To: Selecting random record from MySQL database table.

#2 girasquid  Icon User is offline

  • Barbarbar
  • member icon

Reputation: 108
  • View blog
  • Posts: 1,825
  • Joined: 03-October 06

Re: Selecting random record from MySQL database table.

Posted 22 May 2008 - 03:00 PM

here's a link, and here's your solution.
Was This Post Helpful? 0
  • +
  • -

#3 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4337
  • View blog
  • Posts: 12,137
  • Joined: 18-April 07

Re: Selecting random record from MySQL database table.

Posted 22 May 2008 - 03:26 PM

Well from your code there you pull across all the questions for a specific quiz... good. Now you just need to loop through the results and put the results into an array of questions along with their answers. Once you get them into an array then you can apply all sorts of functions on the array to mix up the questions into a random order. One way to do this is to create two random numbers (using rand()) and find those positions in the array and swap the values. Do this as many times as you like and in no time the array will be randomized.

Then just loop through the array to print the questions. Here is a demo for you...

<?php
	$questions = array();

	// Create our array of questions (which we fetched from the database)
	$questions[0] = array("id" => 1,"question" => "Who is a better super hero, superman or spiderman?", "answer" => "superman");
	$questions[1] = array("id" => 2,"question" => "Daredevil lacks what major sense?", "answer" => "sight");
	$questions[2] = array("id" => 3,"question" => "When did Columbus discover america?", "answer" => "1492");
	$questions[3] = array("id" => 4,"question" => "What is Avogadro's number?", "answer" => "6.0221415 x 10^23");
	$questions[4] = array("id" => 5,"question" => "What is the tallest mountain in the world?", "answer" => "Mt. Everest");


	// List the questions before our randomizing
	echo "Before shuffling...<br/><br/>";

	for ($i = 0; $i < 5; $i++) {
		echo "Question is: " . $questions[$i]["question"] . "<br/>";
	}


	// Call our function to do some random swapping
	mixup($questions);


	// Show that they are mixed up
	echo "<br/>After shuffling...<br/><br/>";

	for ($i = 0; $i < 5; $i++) {
		echo "Question is: " . $questions[$i]["question"] . "<br/>";
	}

	function mixup(&$arValues) {
		// Do 30 random swaps
		for ($i = 0; $i < 30; $i++) {
			// Pick some random elements
			$rand1 = rand(0,count($arValues) - 1);
			$rand2 = rand(0,count($arValues) - 1);

			// Swap them
			$temp = $arValues[$rand1];
			$arValues[$rand1] = $arValues[$rand2];
			$arValues[$rand2] = $temp;
		}
	}
?>



It shows that we create some questions, put them into an array, randomize them up and then spit them out onto the page again in random order.

Or of course you can use the database to do the randomizing for you, but then you lack a bit of the customizability of determine how random they are etc. Either way will work.

Enjoy!

"At DIC we be array swapping code ninjas.... and that is ALL we swap." :snap:

This post has been edited by Martyr2: 22 May 2008 - 03:27 PM

Was This Post Helpful? 0
  • +
  • -

#4 WishAngel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 22-May 08

Re: Selecting random record from MySQL database table.

Posted 23 May 2008 - 10:06 AM

Quote

Or of course you can use the database to do the randomizing for you, but then you lack a bit of the customizability of determine how random they are etc.


I`m not so sure if you guys get the point...but ... how can I use the database to do the randomizing if I want the randomizing to return ALL 20 question random displayed ?...and the questions not to be duplicated....
Thank you, girasquid for that link but i`ve knew it, and the examples from there shows you how to return just one random row from the database...amd I want to return all 20 rows from the database, random and not duplicated... please enlight me...i`m such a noob :(
Was This Post Helpful? 0
  • +
  • -

#5 girasquid  Icon User is offline

  • Barbarbar
  • member icon

Reputation: 108
  • View blog
  • Posts: 1,825
  • Joined: 03-October 06

Re: Selecting random record from MySQL database table.

Posted 23 May 2008 - 11:50 AM

Have you ever used the DISTINCT and LIMIT clauses?

You can modify what the LIMIT clause is set to to pull all 20 back, and use the DISTINCT clause to make sure you're only getting distinct questions back.
Was This Post Helpful? 0
  • +
  • -

#6 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4337
  • View blog
  • Posts: 12,137
  • Joined: 18-April 07

Re: Selecting random record from MySQL database table.

Posted 23 May 2008 - 12:26 PM

I know what you are talking about WishAngel, you want to pull all 20 questions and randomly display them so that you have a random quiz. I am telling you that you are going to pull in all 20 records and randomize them yourself (this will prevent duplicates) or you can use girasquids method of pulling each random record from the database and eliminating duplicates as you go until you have all 20.

Go back through our recommendations and re-read. This may make more sense. :)
Was This Post Helpful? 0
  • +
  • -

#7 WishAngel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 22-May 08

Re: Selecting random record from MySQL database table.

Posted 23 May 2008 - 04:33 PM

Thank you Guys! I used girasquid method...and it works! Thank you again! >:D< >:D< >:D<
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1