Jump to content

How to display question set randomly without repeat each time from 4 tables using PHP & MySQL?


AT-2013

Recommended Posts

Hi!! I am new to PHP & i need to submit a project on online admission system where the question sets should be displayed randomly each time a user logged in from 4 tables which implies subject table. I need the idea immediately & if possible Plz!! Help me 2 get a script or any demonstration about it. I have prepared a script but nt working according to the requirement. Thnx.

Link to comment
Share on other sites

code?

 

Yes, where is your code? We have no idea what your table structure is or how you need to query the questions/answers. I'm not really sure if you are wanting the questions in a random order or the possible answers in a random order. But, my answer will be the same. Since a quiz/test will be a relatively short list of records, I would query the database to get a list of all the questions and associated answers. Then I would dump the results into an array and use shuffle() to randomize with the order of the question, the order of the answers, or both.

Link to comment
Share on other sites

Guru! Wt you have mentioned is perfect. My question sets will alwyz come randomly nt d answers when each a new user enters into the exam. Like I should have a subject table consisting of Physics, Maths, Computer, General knowledge & questions are needed to b set in the database before. Each time the questions sets are needed to come randomly from the table. I have made a script but not working according to the need. If possible any modification to the following codes will be appreciated.

 

 

 

books.zip

completed.zip

ms.zip

subjects.zip

question5.zip

ques.zip

Link to comment
Share on other sites

Guru! Wt you have mentioned is perfect. My question sets will alwyz come randomly nt d answers when each a new user enters into the exam. Like I should have a subject table consisting of Physics, Maths, Computer, General knowledge & questions are needed to b set in the database before. Each time the questions sets are needed to come randomly from the table. I have made a script but not working according to the need. If possible any modification to the following codes will be appreciated. Thnx every1 4 reply.

 

 

 

books.zip

completed.zip

ms.zip

subjects.zip

question5.zip

ques.zip

Link to comment
Share on other sites

My name is not "Guru", my name is "Psycho". "Guru" is a tag that members can earn in this forum if they are productive and helpful here.

 

Looking at the questions table structure you have the following columns:

`ques_id`: The question ID
`q` : The question text
`op1`: Answer #1
`op2`: Answer #2
`op3`: Answer #3
`op4`: Answer #4
`cop`: The correct answer (1-4)

 

If you will never, ever need more than four answers this will be fine. But, to be truly robust the answers should be in a separate table. You also have a subjects table, but there is no "subject" identifier for the questions. If you are going to have different questions for different subjects you will want a subject_id column in the questions table. So, assuming you have done that and you know the subject for which you want to display the questions and answers - this should work:

 

 

//Determine which subject to display the questions for
$subject_id = intval($_GET['subject']);

$query = "SELECT ques_id, q, op1, op2, op3, op4
          FROM question5
          WHERE subject_id = {$subject_id}";
$result = mysql_query($query) or die(mysql_error());

//Put results into an array
$questions = array();
while($row = mysql_fetch_assoc($result))
{
    //Put answers into a temp array
    $answers = array($row['op1'], $row['op2'], $row['op3'], $row['op4']);
    //Randomize the questions array
    shuffle($answers);
    //Add the question and randomized answers to the $questions array
    $questions[] = array(
        'ques_id' => $row['ques_id'],
        'q' => $row['q'],
        'answers' => $answers    
    );
}

//After all questions have been put into an array,
//randomize the order of the questions
shuffle($questions);

//Output the list of questions and answers
$output = "<ol>\n";
foreach($questions as $ques)
{
    $output .= "<li>\n";
    $output .= "{$ques['q']}<br>\n";
    foreach($ques['answers'] as $ans)
    {
        $output .= "<input type='radio' name='answer[{$ques['ques_id']}]' value='{$ans}'> {$ans}<br>\n";
    }
    $output .= "</li><br>\n";
}
$output .= "</ol>\n";

echo $output;

If you do not need questions for different subjects and there is only one set of questions, just remove the WHERE clause in the query.

Edited by Psycho
Link to comment
Share on other sites

Thnx a lot Psycho 4 your msg. It's jst working fine. You have done it wid radio buttons but I've jst used links to submit an answer. Now I need to keep track of them means $_SESSION['marks'] = 0 in my code as you saw. How could I use the above idea to mine as the answers are submitted by clicking?? Your code just shuffles all the stuffs in the question also the answers. If possible can you help a little to make understand that with my coding style & how to keep the tracks of answers?? Thnx again.

Link to comment
Share on other sites

Hi Frndz!! It's me again. I have created a simple quiz system but the problem is the questions aren't in random order. If possible need help about how to make the questions appear randomly each time & once. I have tried to do so but failed. I am sending the codes & tables below: (create a database named 'online') Thnx in advance.

 

 

 

 

 

 

 

Edited by AT-2013
Link to comment
Share on other sites

Hi Frndz!! It's me again. I have created a simple quiz system but the problem is the questions aren't in random order. If possible need help about how to make the questions appear randomly each time & once. I have tried to do so but failed. I am sending the codes & tables below: (create a database named 'online') Thnx in advance.

 

 

examserver_modelTest.php

exam_login1.php

members1.php

grammar.zip

online2.zip

scores.zip

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.