doforumda Posted October 9, 2009 Share Posted October 9, 2009 hi i need following html code to be converted into php. i have an html page with five MCQs. so i want these questions loads from mysql table and formats exact the same way as here. and also by selecting the correct answer it should says it is correct. also tell me how can i make mysql table for this? <html> <head> <title>Take the Quiz!</title> </head> <body> <h1>Find Out you computer knowledge</h1> <form action="quizaction.php" method="post"> <b>Question 1: What is the only function all C++ programs must contain?</b><br> <input type="radio" name="q1" value="start"> start() <br> <input type="radio" name="q1" value="system"> system() <br> <input type="radio" name="q1" value="main"> main() <br> <input type="radio" name="q1" value="program"> program() <br> <b>Question 2: What punctuation is used to signal the beginning and end of code blocks?</b><br> <input type="radio" name="q2" value="braces"> { } <br> <input type="radio" name="q2" value="minuGreater"> -> and <- <br> <input type="radio" name="q2" value="begin"> BEGIN and END <br> <input type="radio" name="q2" value="paren"> ( ) <br> <b>Question 3: What punctuation ends most lines of C++ code?</b><br> <input type="radio" name="q3" value="dot"> "." dot <br> <input type="radio" name="q3" value="semi-colon"> ";" semi-colon <br> <input type="radio" name="q3" value="colon"> ":" colon <br> <input type="radio" name="q3" value="single-quotes"> "'" single quotes <br> <b>Question 4: Which of the following is a correct comment?</b><br> <input type="radio" name="q4" value="*/"> */comment*/ <br> <input type="radio" name="q4" value="**"> **comment** <br> <input type="radio" name="q4" value="/*"> /*comment*/ <br> <input type="radio" name="q4" value="[*"> [*comment*] <br> <b>Question 5: Which of the following is not a correct variable type?</b><br> <input type="radio" name="q5" value="float"> float <br> <input type="radio" name="q5" value="int"> int <br> <input type="radio" name="q5" value="double"> double <br> <input type="radio" name="q5" value="real"> real <br> <br> <input type="submit" value="Submit Quiz"> </form> </body> </html> php code is here if needed <?php $score = 0; if ($_POST['q1'] == 'main') $score++; if ($_POST['q2'] == 'braces') $score++; if ($_POST['q3'] == 'semi-colon') $score++; if ($_POST['q4'] == '/*') $score++; if ($_POST['q5'] == 'real') $score++; ?> <html> <head> <title>Your Results of the Quiz!</title> </head> <body> <h1>Your Results for Quiz!</h1> <?php echo '<b>Your score was ' . $score . '/5</b><br><br>'; if ($score < 3) echo 'your score is not good!'; else if ($score == 5) echo 'Congratulations'; else echo 'you scored average'; ?> </body> </html> Link to comment https://forums.phpfreaks.com/topic/177055-need-help-in-converting-html-to-php/ Share on other sites More sharing options...
trq Posted October 9, 2009 Share Posted October 9, 2009 What 'help' do you need exactly? Where not here to do it for you. Link to comment https://forums.phpfreaks.com/topic/177055-need-help-in-converting-html-to-php/#findComment-933538 Share on other sites More sharing options...
PugJr Posted October 9, 2009 Share Posted October 9, 2009 To make a table, go to your phpmyadmin and create a DB then create a table. To insert a value into the DB: Table being named quiz. Columns id, cplusplus, punctuation, ends, comment, variable, correct, $cplusplus = $_POST['q1']; /// Repeat with the rest if ( $cplusplus != "main" ) { $cplusplus = 1; } else { $cplusplus = 0; } /// Repeat and do that with the other 5 questions. $correct = $cplusplus + $punctuation .... /// Here too mysql_query("INSERT INTO quiz (id, cplusplus, punctuation, ends, comment, variable, correct,) VALUES('$npcnewid', '$cplusplus', '$punctuation', ...) ") or die(mysql_error()); /// Need to finish it here Or if you like we could have an array instead of separate columns. Link to comment https://forums.phpfreaks.com/topic/177055-need-help-in-converting-html-to-php/#findComment-933540 Share on other sites More sharing options...
doforumda Posted October 9, 2009 Author Share Posted October 9, 2009 i want that html form to be dynamic. it should get questions from a mysql db and place the way it is right now. how can i do that? Link to comment https://forums.phpfreaks.com/topic/177055-need-help-in-converting-html-to-php/#findComment-933542 Share on other sites More sharing options...
PugJr Posted October 9, 2009 Share Posted October 9, 2009 Okay make a table called "questions" or something like that. Under questions you have columns "id, question, answerone, answertwo, answerthree, answerfour" $queryquestions = "SELECT * FROM questions" or die(); $resultquestions = mysql_query($queryquestions) or die(); while($rowquestions = mysql_fetch_array($resultquestions)){ $questionid = $rowquestions['id']; $question = $rowquestions['question']; $answerone = $rowquestions['answerone']; $answertwo = $rowquestions['answertwo']; $answerthree = $rowquestions['answerthree']; $answerfour = $rowquestions['answerfour']; echo " <form action=\"quizaction.php\" method=\"post\"> <b>Question $id: $question</b><br> <input type=\"radio\" name=\"$id\" value=\"1\"> $answerone <br> <input type=\"radio\" name=\"$id\" value=\"2\"> $answertwo <br> <input type=\"radio\" name=\"$id\" value=\"3\"> $answerthree <br> <input type=\"radio\" name=\"$id\" value=\"4\"> $answerfour <br>"; } Ultimately what that does is takes every question in table "questions", and then displays them in your format. Link to comment https://forums.phpfreaks.com/topic/177055-need-help-in-converting-html-to-php/#findComment-933544 Share on other sites More sharing options...
doforumda Posted October 9, 2009 Author Share Posted October 9, 2009 yes it is almost correct but it also displays this "Notice: Undefined variable: id in D:\wamp\www\feedback\quiz2.php on line 28 Notice: Undefined variable: id in D:\wamp\www\feedback\quiz2.php on line 29 Notice: Undefined variable: id in D:\wamp\www\feedback\quiz2.php on line 30 Notice: Undefined variable: id in D:\wamp\www\feedback\quiz2.php on line 31 Notice: Undefined variable: id in D:\wamp\www\feedback\quiz2.php on line 32 Question : What is the only function all C++ programs must contain? start() system() main() program() "; Link to comment https://forums.phpfreaks.com/topic/177055-need-help-in-converting-html-to-php/#findComment-933555 Share on other sites More sharing options...
PugJr Posted October 9, 2009 Share Posted October 9, 2009 Oops, I see my mistake. <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>"; Replace that one with your previous. Link to comment https://forums.phpfreaks.com/topic/177055-need-help-in-converting-html-to-php/#findComment-933558 Share on other sites More sharing options...
doforumda Posted October 9, 2009 Author Share Posted October 9, 2009 thanks i may need some more help later Link to comment https://forums.phpfreaks.com/topic/177055-need-help-in-converting-html-to-php/#findComment-933562 Share on other sites More sharing options...
PugJr Posted October 9, 2009 Share Posted October 9, 2009 Uh oh! I left a "\" from one of the radios. Thats gonna cause an error. Better fix it. Do realize what I have given won't update your database though, it'll only put form values for your questions. Link to comment https://forums.phpfreaks.com/topic/177055-need-help-in-converting-html-to-php/#findComment-933563 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.