PHP MySQL Quiz

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
nithinkk
Forum Commoner
Posts: 55
Joined: Sat Nov 28, 2009 7:57 am

PHP MySQL Quiz

Post by nithinkk »

I had setup a quiz database which has around 50 questions in it with answers. I need to display one question at a time from database with next and previous button. Can anyone suggest how to start ? i want one question with for options to display in one page and when user clicks the next question shoud come.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: PHP MySQL Quiz

Post by social_experiment »

You can google 'Retrieving information from a SQL database using php' or search this forum; The principles behind accessing the database (and getting the information) is the same for most php applications so you're likely to find some good resources
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
nithinkk
Forum Commoner
Posts: 55
Joined: Sat Nov 28, 2009 7:57 am

Re: PHP MySQL Quiz

Post by nithinkk »

Using the below code i manage to display one questions at a time. Can any one suggest how to manage the answers that user selects ?

Code: Select all

 

<?php

	if(isset($_POST['next'])){
		$a=$_POST['a'];
	}
	if(!isset($a)){
		$a=0;
	}
     
	include('common/connect_quiz.php');
	$sql1="SELECT * FROM questions WHERE quiz_details_Id='1' LIMIT 1 OFFSET $a";
	$result=mysql_query($sql1);
	echo "<form method='post' action=''>";
	while ($row = mysql_fetch_array($result))
	{
		echo $row['question']. "<br/>";
		echo "<input type='radio' value='1' name='answer'>" .$row['opt_1']. "<br/>";
		echo "<input type='radio' value='2' name='answer'>" .$row['opt_2']. "<br/>";
		echo "<input type='radio' value='3' name='answer'>" .$row['opt_3']. "<br/>";
		echo "<input type='radio' value='4' name='answer'>" .$row['opt_4']. "<br/>";
	}
	$a=$a+1;
	echo "<input type='hidden' value='$a' name='a'>";
	echo "<input type='submit' name='next' value='next'> ";
	echo "</form>";
	


?> 

User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: PHP MySQL Quiz

Post by social_experiment »

Assuming that you have the answers written in the database, you have to compare the answer given by the user with the value written in the database. Do you tie the question to the database with the value in $a?
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
nithinkk
Forum Commoner
Posts: 55
Joined: Sat Nov 28, 2009 7:57 am

Re: PHP MySQL Quiz

Post by nithinkk »

Yes my table design is as follows

+------------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| quiz_title | varchar(64) | NO | | NULL | |
| quiz_description | varchar(100) | NO | | NULL | |
+------------------+--------------+------+-----+---------+----------------+

+-----------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+--------------+------+-----+---------+----------------+
| question_id | int(11) | NO | PRI | NULL | auto_increment |
| quiz_details_id | int(11) | NO | MUL | NULL | |
| question | varchar(100) | YES | | NULL | |
| opt_1 | varchar(60) | YES | | NULL | |
| opt_2 | varchar(60) | YES | | NULL | |
| opt_3 | varchar(60) | YES | | NULL | |
| opt_4 | varchar(60) | YES | | NULL | |
| opt_ans | int(4) | YES | | NULL | |
+-----------------+--------------+------+-----+---------+----------------+
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: PHP MySQL Quiz

Post by social_experiment »

Code: Select all

+-----------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+--------------+------+-----+---------+----------------+
| question_id | int(11) | NO | PRI | NULL | auto_increment | 
| quiz_details_id | int(11) | NO | MUL | NULL | | 
| question | varchar(100) | YES | | NULL | | 
| opt_1 | varchar(60) | YES | | NULL | | 
| opt_2 | varchar(60) | YES | | NULL | | 
| opt_3 | varchar(60) | YES | | NULL | | 
| opt_4 | varchar(60) | YES | | NULL | | 
| opt_ans | int(4) | YES | | NULL | | 
+-----------------+--------------+------+-----+---------+----------------+
what is the function of field opt_1 to opt_4? If they are for answers; you only need the correct answer stored in the database
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
nithinkk
Forum Commoner
Posts: 55
Joined: Sat Nov 28, 2009 7:57 am

Re: PHP MySQL Quiz

Post by nithinkk »

No they are to store the options. ie

For Example :


What is phishing? (question)

1 ) A computer virus (opt_1)
2 ) A corrupt website (opt_2)
3) A distribution of spam (opt_3)
4 ) An identity theft scheme (opt_4)

Ans : 4) (opt_ans)
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: PHP MySQL Quiz

Post by social_experiment »

My bad :)

I'd recommend a separate table for the answers; it would have an id, question_id (this acts as a foreign key) and opt_ans (containing the answer)

Code: Select all

<?php
 // $question id
 $qID = $_POST['a'];

 // SQL query
 $qry = "SELECT COUNT(`id`) FROM `question_tbl` WHERE `question_id` = '" . $qID . "' AND `opt_ans` = '" . $_POST['answer'] . "' ";
 $sql  = mysql_query($qry);
 $ary = mysql_fetch_array($sql);
 $rows = $ary[0];

 if ($rows == 1) { 
   // question answered correctly
 }
 else {
  // wrong answer
 }
?>
Use this as guidance for creating your own script

Hth
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
nithinkk
Forum Commoner
Posts: 55
Joined: Sat Nov 28, 2009 7:57 am

Re: PHP MySQL Quiz

Post by nithinkk »

Thanks a lot !

I had made changes in database design. Now my proble is to shuffel or show the questions in a random order. But when i try order by rand() the questions are repeating . Can any one suggest what to do ???? Below is my code...

Code: Select all


$sql1="SELECT * FROM questions WHERE quiz_details_Id='2' LIMIT 1 OFFSET $a";
			$result=mysql_query($sql1);
			echo "<form method='post' action=''>";
			while ($row = mysql_fetch_array($result))
				{
		
		
				$question_id=htmlentities($row['question_id']);
				$quiz_id=htmlentities($row['quiz_details_id']);
				$qry = "SELECT * FROM `answers` WHERE que_id='$question_id'";  
 				$sql  = mysql_query($qry);
 				$ary = mysql_fetch_array($sql);
 				$real_answer = $ary[2];
		
		
				echo "<h1 class='header'>".$b.") ".$row['question']."</h1>";
				echo "<input type='radio' value='1' name='answer'>" .htmlentities($row['opt_1']). "<br/><br/>";
				echo "<input type='radio' value='2' name='answer'>" .htmlentities($row['opt_2']). "<br/><br/>";
				echo "<input type='radio' value='3' name='answer'>" .htmlentities($row['opt_3']). "<br/><br/>";
				echo "<input type='radio' value='4' name='answer'>" .htmlentities($row['opt_4']). "<br/><br/>";
		
	
				}

I also tried

Code: Select all

 $sql1="SELECT Distint * FROM questions WHERE quiz_details_Id='2'  ORDER BY RAND()  LIMIT 1 OFFSET $a";  
But the questions are repeting :-(
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: PHP MySQL Quiz

Post by Celauran »

There's no real point using DISTINCT with LIMIT 1; you can't possibly get duplicate values if you're only returning a single row. The obvious solution would be to have multiple questions in the same form. Barring that, you could store the ID of the questions already asked in session data and add a clause to your query WHERE id NOT IN (id_of_asked_question)
nithinkk
Forum Commoner
Posts: 55
Joined: Sat Nov 28, 2009 7:57 am

Re: PHP MySQL Quiz

Post by nithinkk »

No i want only one question at a time ! Can we shuffel or change the order of questions retriving by diffrent users. Now all users will get same pattern.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: PHP MySQL Quiz

Post by social_experiment »

What about selecting a random number from an array and depending on that return the corresponding question; The random value is used for the question id:

Code: Select all

 $qry = "SELECT fields FROM table WHERE questionID = '" . $randomQuestionIDValue . "' ";
You would have to check that once a value is selected it cannot be used again; this will stop question appearing multiple times.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: PHP MySQL Quiz

Post by Celauran »

nithinkk wrote:No i want only one question at a time ! Can we shuffel or change the order of questions retriving by diffrent users. Now all users will get same pattern.
Yes, I already explained how to do that. Order by random, and use session data to keep track of questions they've already had. Easy peasy.
nithinkk
Forum Commoner
Posts: 55
Joined: Sat Nov 28, 2009 7:57 am

Re: PHP MySQL Quiz

Post by nithinkk »

Your solution is not clear for me....Can you tell what should i do in the code ?

Code: Select all

//quiz_details_id is same for all questions..in this case 1 is the id.
$sql1="SELECT * FROM questions WHERE quiz_details_Id='1' order by rand() LIMIT 1 OFFSET $a";
                        $result=mysql_query($sql1);
                        echo "<form method='post' action=''>";
                        while ($row = mysql_fetch_array($result))
                                {
               
               
                                $question_id=htmlentities($row['question_id']);
                                $quiz_id=htmlentities($row['quiz_details_id']);
                                $qry = "SELECT * FROM `answers` WHERE que_id='$question_id'";  
                                $sql  = mysql_query($qry);
                                $ary = mysql_fetch_array($sql);
                                $real_answer = $ary[2];
               
               
                                echo "<h1 class='header'>".$b.") ".$row['question']."</h1>";
                                echo "<input type='radio' value='1' name='answer'>" .htmlentities($row['opt_1']). "<br/><br/>";
                                echo "<input type='radio' value='2' name='answer'>" .htmlentities($row['opt_2']). "<br/><br/>";
                                echo "<input type='radio' value='3' name='answer'>" .htmlentities($row['opt_3']). "<br/><br/>";
                                echo "<input type='radio' value='4' name='answer'>" .htmlentities($row['opt_4']). "<br/><br/>";
               
       
                                }

Should i create a session array and insert the id in to it and check whether the id is used or not ? But i dont know how to do it ! Can you tell me what to do ?
Post Reply