Jump to content

need help in php code


doforumda

Recommended Posts

hi i have a problem in php code. i want to make dynamic quiz mcq based. i create a table with following columns "id, question, option1,option2,option3,option4, correctoption". all these information is retrieved from the database using following code.

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Document</title>
</head>

<body>
<p>
<form name="form1" method="post" action="quiz2action.php">
  <?php

$db = mysql_connect("localhost");
mysql_select_db("videoshop", $db);

$queryquestions = "SELECT * FROM quiz" or die(); 
$resultquestions = mysql_query($queryquestions) or die();

while($rowquestions = mysql_fetch_array($resultquestions))
{

$questionid = $rowquestions['id'];
$question = $rowquestions['question'];
$answerone = $rowquestions['option1'];
$answertwo = $rowquestions['option2'];
$answerthree = $rowquestions['option3'];
$answerfour = $rowquestions['option4'];
echo "
<form action=\"quizaction.php\" method=\"post\">

 <b>Question $questionid: $question</b><br>
<input type=\"radio\" name=\"$questionid\" value=\"1\"> $answerone <br>
<input type=\"radio\" name=\"$questionid\" value=\"2\"> $answertwo <br>
<input type=\"radio\" name=\"$questionid\" value=\"3\"> $answerthree <br>
<input type=\"radio\" name=\"$questionid\" value=\"4\"> $answerfour <br>";

echo "<br><br>";

}

?>
</p>

  <label>
  <input type="submit" name="button" id="button" value="Submit">
  </label>
</form>
<p> </p>
</body>
</html>

 

so through above code data is formated on the page just like on other mcqs websites. now what i want to do is when the click submit button on other page the user answers should be compared with the column "correctanswer" in the table if the comparison gets true then $score++.

how can i achieve this?

Link to comment
Share on other sites

First, you don't have an option for score in your table, is that being saved in another table?  The basic concept is pretty simple.  Once you submit the form, you must check the form value.  So you may have something like:

 

$question_id = $_POST['questionid'];

$answer_given = $_POST['answer'];

 

Then you run a query in your database to see if that matches the correct answer

 

SELECT * FROM table WHERE questionid = $question_id and correctionoption = $answer_given;

 

If a result is found you can increase the player score by one, however that is stored.

Link to comment
Share on other sites

list($c_answer)=mysql_fetch_row(mysql_query("SELECT correctoption FROM quiz WHERE id='$_POST[question]' LIMIT 1")) or die();

if ($c_answer==$_POST[answer]){

//correct, continue

$score++;

}

 

in your form have

<INPUT TYPE='hidden' NAME='question' VALUE='$questionid'>

 

and change your radio buttons to

<INPUT TYPE='radio' NAME='answer' VALUE='1'>

and so on

Link to comment
Share on other sites

i still have problems first of all i dont understand $_POST['answer'] what does that answer mean and second is i get these errors

 

Notice: Undefined index: questionid in D:\wamp\www\feedback\quiz2action.php on line 2

 

Notice: Undefined index: questionid in D:\wamp\www\feedback\quiz2action.php on line 10

Link to comment
Share on other sites

on other page i am trying to do this

 

<?php
$question_id = $_POST['questionid'];
$answer_given = $_POST['answer'];

$db = mysql_connect("localhost");
mysql_select_db("videoshop", $db);
$query = "SELECT * FROM table WHERE id = ".$question_id." and correct = ".$answer_given."";
echo $query;
$result = mysql_query($query);

$score = 0;

if($result)
{
$score++;
}

 

but this does not work

Link to comment
Share on other sites

what this means

 

in your form have

<INPUT TYPE='hidden' NAME='question' VALUE='$questionid'>

 

and change your radio buttons to

<INPUT TYPE='radio' NAME='answer' VALUE='1'>

 

first of all i dont see no such thing in my form as <INPUT TYPE='hidden' NAME='question' VALUE='$questionid'> and second you mean i should change this line with below input

Link to comment
Share on other sites

ok, full script:

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Document</title>
</head>

<body>
<p>
<form name="form1" method="post" action="quiz2action.php">
  <?php

$db = mysql_connect("localhost");
mysql_select_db("videoshop", $db);

$queryquestions = "SELECT * FROM quiz" or die(); 
$resultquestions = mysql_query($queryquestions) or die();

while($rowquestions = mysql_fetch_array($resultquestions))
{
   
   $questionid = $rowquestions['id'];
   $question = $rowquestions['question'];
   $answerone = $rowquestions['option1'];
   $answertwo = $rowquestions['option2'];
   $answerthree = $rowquestions['option3'];
   $answerfour = $rowquestions['option4'];
   echo "
   <form action=\"quizaction.php\" method=\"post\">
   
    <b>Question $questionid: $question</b><br>
   <input type=\"radio\" name=\"question[$questionid]\" value=\"1\"> $answerone <br>
   <input type=\"radio\" name=\"question[$questionid]\" value=\"2\"> $answertwo <br>
   <input type=\"radio\" name=\"question[$questionid]\" value=\"3\"> $answerthree <br>
   <input type=\"radio\" name=\"question[$questionid]\" value=\"4\"> $answerfour <br>";
   
   echo "<br><br>";

}

?>
</p>

  <label>
  <input type="submit" name="button" id="button" value="Submit">
  </label>
</form>
<p> </p>
</body>
</html>

 

then in quiz2action.php

 

<?php
$db = mysql_connect("localhost");
mysql_select_db("videoshop", $db);

$score = 0;

foreach ($_POST['question'] as $key => $value){
$query = "SELECT * FROM table WHERE id = '".$key."' and correct = '".$value."'";
$result = mysql_query($query);
if($result){
	 $score++;
}
}

 

Link to comment
Share on other sites

yes it is working but not completely working. when i select correct option it does not increase my score.

 

and when i echo my query it says

 

SELECT * FROM table WHERE id = '1' and correct = '3'

SELECT * FROM table WHERE id = '2' and correct = '3'

 

but when the it exists then it should also increase my score.

Link to comment
Share on other sites

ok ameyemad it is working now your full script was correct. there is another problem now. this time when i select incorrect option this script still says correct answer whereas it is incorrect and it increases my score.

 

i also made some modification in that script which you can see

 

$db = mysql_connect("localhost");
mysql_select_db("videoshop", $db);

$score = 0;

foreach ($_POST['question'] as $key => $value){
   $query = "SELECT * FROM quiz WHERE id = '".$key."' and correct = '".$value."'";
   echo "<BR>";
   echo $query;
   $result = mysql_query($query);
   if($result){
       $score++;
   }
}
echo "<br>";
echo "your score is ".$score."/5";

Link to comment
Share on other sites

still same problem i change the values in correct column as you said but still have the same problem

 

it displays the following output on second script

 

SELECT * FROM quiz WHERE id=1 AND correct=2

SELECT * FROM quiz WHERE id=2 AND correct=3

SELECT * FROM quiz WHERE id=3 AND correct=1

SELECT * FROM quiz WHERE id=4 AND correct=3

SELECT * FROM quiz WHERE id=5 AND correct=2

 

your score is 5/5

Link to comment
Share on other sites

try this alternate code

 

$db = mysql_connect("localhost");
mysql_select_db("videoshop", $db);

$score = 0;

foreach ($_POST['question'] as $key => $value){
   $query = "SELECT correct FROM quiz WHERE id = '".$key."' and correct = '".$value."'";
   echo "<BR>";
   echo $query;
   $result = mysql_query($query);
   if($result[correct]){
       $score++;
   }
}
echo "<br>";
echo "your score is ".$score."/5";

 

have you got this page up somewhere for viewing?

Link to comment
Share on other sites

i find what was the problem. that if condition should be like this

 

if($result && mysql_num_rows($result))

{

      $score++;

}

 

now this quiz website is almost completed except one thing and that is timer. i want to a five minutes timer. the user will have five minutes to complete the quiz if user doesnt succeed in completing quiz in given time then quiz should disable and shows him his result. that timer should appear on the page.

how this can be achieve

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.