Welcome to Dream.In.Code
Getting PHP Help is Easy!

Join 135,948 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 2,706 people online right now. Registration is fast and FREE... Join Now!




Selecting random record from MySQL database table.

 
Reply to this topicStart new topic

Selecting random record from MySQL database table., quizz problems

WishAngel
22 May, 2008 - 12:58 PM
Post #1

New D.I.C Head
*

Joined: 22 May, 2008
Posts: 3

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:

CODE
$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 sad.gif >:D< Thanks in advance! Please help sad.gif...
User is offlineProfile CardPM
+Quote Post

girasquid
RE: Selecting Random Record From MySQL Database Table.
22 May, 2008 - 02:00 PM
Post #2

Barbarbar
Group Icon

Joined: 3 Oct, 2006
Posts: 1,264



Thanked: 14 times
Dream Kudos: 650
My Contributions
here's a link, and here's your solution.
User is online!Profile CardPM
+Quote Post

Martyr2
RE: Selecting Random Record From MySQL Database Table.
22 May, 2008 - 02:26 PM
Post #3

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,173



Thanked: 208 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
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...

CODE

<?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." decap.gif

This post has been edited by Martyr2: 22 May, 2008 - 02:27 PM
User is offlineProfile CardPM
+Quote Post

WishAngel
RE: Selecting Random Record From MySQL Database Table.
23 May, 2008 - 09:06 AM
Post #4

New D.I.C Head
*

Joined: 22 May, 2008
Posts: 3

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 sad.gif
User is offlineProfile CardPM
+Quote Post

girasquid
RE: Selecting Random Record From MySQL Database Table.
23 May, 2008 - 10:50 AM
Post #5

Barbarbar
Group Icon

Joined: 3 Oct, 2006
Posts: 1,264



Thanked: 14 times
Dream Kudos: 650
My Contributions
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.
User is online!Profile CardPM
+Quote Post

Martyr2
RE: Selecting Random Record From MySQL Database Table.
23 May, 2008 - 11:26 AM
Post #6

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,173



Thanked: 208 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
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. smile.gif
User is offlineProfile CardPM
+Quote Post

WishAngel
RE: Selecting Random Record From MySQL Database Table.
23 May, 2008 - 03:33 PM
Post #7

New D.I.C Head
*

Joined: 22 May, 2008
Posts: 3

Thank you Guys! I used girasquid method...and it works! Thank you again! >:D< >:D< >:D<
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/1/08 08:57AM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month