doforumda Posted October 9, 2009 Share Posted October 9, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/177081-need-help-in-php-code/ Share on other sites More sharing options...
jcombs_31 Posted October 9, 2009 Share Posted October 9, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/177081-need-help-in-php-code/#findComment-933676 Share on other sites More sharing options...
ameyemad Posted October 9, 2009 Share Posted October 9, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/177081-need-help-in-php-code/#findComment-933680 Share on other sites More sharing options...
doforumda Posted October 9, 2009 Author Share Posted October 9, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/177081-need-help-in-php-code/#findComment-933691 Share on other sites More sharing options...
doforumda Posted October 9, 2009 Author Share Posted October 9, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/177081-need-help-in-php-code/#findComment-933723 Share on other sites More sharing options...
doforumda Posted October 9, 2009 Author Share Posted October 9, 2009 i am still waiting for help Quote Link to comment https://forums.phpfreaks.com/topic/177081-need-help-in-php-code/#findComment-933829 Share on other sites More sharing options...
ameyemad Posted October 9, 2009 Share Posted October 9, 2009 did you try my example above as well? Quote Link to comment https://forums.phpfreaks.com/topic/177081-need-help-in-php-code/#findComment-933953 Share on other sites More sharing options...
doforumda Posted October 10, 2009 Author Share Posted October 10, 2009 yes ameyemad i also tried your example Quote Link to comment https://forums.phpfreaks.com/topic/177081-need-help-in-php-code/#findComment-934117 Share on other sites More sharing options...
doforumda Posted October 10, 2009 Author Share Posted October 10, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/177081-need-help-in-php-code/#findComment-934134 Share on other sites More sharing options...
ameyemad Posted October 10, 2009 Share Posted October 10, 2009 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++; } } Quote Link to comment https://forums.phpfreaks.com/topic/177081-need-help-in-php-code/#findComment-934141 Share on other sites More sharing options...
doforumda Posted October 10, 2009 Author Share Posted October 10, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/177081-need-help-in-php-code/#findComment-934191 Share on other sites More sharing options...
doforumda Posted October 10, 2009 Author Share Posted October 10, 2009 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"; Quote Link to comment https://forums.phpfreaks.com/topic/177081-need-help-in-php-code/#findComment-934203 Share on other sites More sharing options...
doforumda Posted October 10, 2009 Author Share Posted October 10, 2009 i have upload an image of my quiz table which can be found on the following link. http://www.mediafire.com/?sharekey=1664d0f04e1bca8f6e7203eb87368129e04e75f6e8ebb871 Quote Link to comment https://forums.phpfreaks.com/topic/177081-need-help-in-php-code/#findComment-934231 Share on other sites More sharing options...
ameyemad Posted October 10, 2009 Share Posted October 10, 2009 you should save "correct" in this table as either 1,2,3 or 4 and not the actual answer. Quote Link to comment https://forums.phpfreaks.com/topic/177081-need-help-in-php-code/#findComment-934249 Share on other sites More sharing options...
doforumda Posted October 10, 2009 Author Share Posted October 10, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/177081-need-help-in-php-code/#findComment-934256 Share on other sites More sharing options...
doforumda Posted October 10, 2009 Author Share Posted October 10, 2009 that if condition always return true even if the answer selected is wrong Quote Link to comment https://forums.phpfreaks.com/topic/177081-need-help-in-php-code/#findComment-934269 Share on other sites More sharing options...
ameyemad Posted October 11, 2009 Share Posted October 11, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/177081-need-help-in-php-code/#findComment-934618 Share on other sites More sharing options...
doforumda Posted October 11, 2009 Author Share Posted October 11, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/177081-need-help-in-php-code/#findComment-934689 Share on other sites More sharing options...
doforumda Posted October 11, 2009 Author Share Posted October 11, 2009 anyone who can help Quote Link to comment https://forums.phpfreaks.com/topic/177081-need-help-in-php-code/#findComment-934789 Share on other sites More sharing options...
cags Posted October 11, 2009 Share Posted October 11, 2009 JavaScript. PHP cannot work as a timer as it is server side code. In order to enforce a timer on the user you would need to use something client side. Which generally means JavaScript. Quote Link to comment https://forums.phpfreaks.com/topic/177081-need-help-in-php-code/#findComment-934817 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.