peppericious Posted July 15, 2012 Share Posted July 15, 2012 I'm trying to create a quiz which will be associated with a particular article my students have read. Using the article_id, ten random questions (and corresponding answers) will be pulled from my db. My plan was to: -- get the 10 Q/As -- assign these to session variables -- compare posted answers to the actual answers in the session variables -- give feedback on completion of the test like "You got X percent." However, it's not working. The questions are being generated fine. Here's an example: http://justbitsandpieces.com/testing/quiz2.php?article_id=7 However, when I post the answers, 2 problems arise, namely: (a) the percentage calculated on completion of the test is wrong, and (b) the function which creates inputs in the form to allow the user to type his/her answers does not make the inputs sticky (to show the user the answers he/she got right and wrong). Can anybody point me to the solution to (a) and (b), above? Thanks in advance for any help. The code for my page is here: <?php /////////////////////////////////////////////////////////////////////////////////////////// //// //// //// The script below creates a 10-question quiz associated with whatever article. //// //// Article id is retrieved from url when page opens. //// //// //// /////////////////////////////////////////////////////////////////////////////////////////// session_start(); include('includes/header.php'); if(isset($_GET['article_id'])) { $article_id = $_GET['article_id']; // function to create form inputs for user to type answers (start) function create_input($name) { if(isset($_POST['$name'])) { // sticky input to retain user's answer $input = "<input type='text' name='" . $name . "' id='" . $name . "' value='" . $_POST['name'] . "' />"; } else { $input = "<input type='text' name='" . $name . "' id='" . $name . "' value='' />"; } return $input; } // create form inputs for user answers (end) // retrieve Qs and As from db include('includes/mysqli_connect.php'); $q = "SELECT question, answer FROM quizzes WHERE f_article_id = $article_id ORDER BY rand() LIMIT 10"; // random 10 questions & answers $r = mysqli_query($dbc, $q); $num_Qs = mysqli_num_rows($r); while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) { $Qs[] = $row['question']; $As[] = $row['answer']; $_SESSION['Q'] = $Qs; $_SESSION['A'] = $As; } if(isset($_POST['submit'])) { // check answers $counter = 0; $correct_answer_count = 0; $incorrect_answer_count = 0; foreach ($_SESSION['A'] as $answer) { if(trim(strtolower($_POST[$counter])) == $answer) { $correct_answer_count++; } else { $incorrect_answer_count++; } $counter++; } // calc percentage and provide feedback $percentage = round($correct_answer_count/$num_Qs*100,2); $feedback = array('Impressive!', 'Neat!', 'Cool!', 'Well done!'); $feedback = $feedback[array_rand($feedback)]; echo "<div class='result'>You answered " . $correct_answer_count . " answers correctly and " . $incorrect_answer_count . " incorrectly. You got " . $percentage . " percent. "; if($percentage >= 80) { echo $feedback; } echo <<<EOT </div> <form id="form1" name="form1" method="post" action=""> <ol> EOT; $counter = 0; foreach ($_SESSION['Q'] as $question) { $input = create_input($counter); $question = str_replace('^', $input, $question); echo "<li>" . $question . "</li>"; $counter++; } echo <<<EOT </ol> <input type='submit' name='submit' id='submit' value='See how I did' /> </form> EOT; } else { /// show form when pages opens /// assemble possible answers to show at top of quiz (start); the user must enter one of these in each blank $poss_answers = ''; $q2 = "SELECT DISTINCT answer FROM quizzes WHERE f_article_id = $article_id ORDER BY rand()"; $r2 = mysqli_query($dbc, $q2); while ($row2 = mysqli_fetch_array($r2, MYSQLI_ASSOC)) { $poss_answers .= "<span class='poss_answers'>" . strtolower($row2['answer']) . "</span>"; } /// possible answers END echo <<<EOT <h2>Commen?ons par ces $num_Qs questions suivantes :</h2> <h3>Tapez l'un des solutions – $poss_answers – dans chacun des blancs.</h3> <form id="form1" name="form1" method="post" action=""><ol> EOT; $counter = 0; foreach ($_SESSION['Q'] as $question) { $input = create_input($counter); $question = str_replace('^', $input, $question); echo "<li>" . $question . "</li>"; $counter++; } echo <<<EOT </ol> <input type='submit' name='submit' id='submit' value='See how I did' /> </form> EOT; /// show form END } } else { // no GET variable so show page access error and quit script echo "<h2>Oops…</h2><p>This page has been accessed in error.</p>"; // show page bottom } ?> </div> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/265716-code-for-quiz-not-working/ Share on other sites More sharing options...
memfiss Posted July 16, 2012 Share Posted July 16, 2012 function create_input($name) { if (isset($_POST['$name'])) { #wrong index if (isset( $_POST[$name] )) { #and may be u needed to put value $_POST[$name] ? #and no needed variable function create_input($name) { if(isset( $_POST[$name] )) return "<input type='text' name='" . $name . "' id='" . $name . "' value='" . $_POST[$name] . "' />"; return "<input type='text' name='" . $name . "' id='" . $name . "' value='' />"; } ... $article_id = $_GET['article_id']; # maybe il put text into get variable ? $article_id = (int)$_GET['article_id']; ... while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) { $Qs[] = $row['question']; $As[] = $row['answer']; $_SESSION['Q'] = $Qs; $_SESSION['A'] = $As; } #no needed variable $_SESSION['Q'] = array(); $_SESSION['A'] = array(); while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) { $_SESSION['Q'] [] = $row['question']; $_SESSION['A'] [] = $row['answer']; } Quote Link to comment https://forums.phpfreaks.com/topic/265716-code-for-quiz-not-working/#findComment-1361889 Share on other sites More sharing options...
peppericious Posted July 16, 2012 Author Share Posted July 16, 2012 Thanks memfiss, but it's still not working. The problem regarding issue (a) is that, when I click submit, I'm overwriting the contents of my session variables so the post data are getting compared to the new/wrong session content... Am still trying to figure out how to fix this. As for issue (b), still can't figure out the 'stickiness' problem: your suggestion didn't resolve it... Quote Link to comment https://forums.phpfreaks.com/topic/265716-code-for-quiz-not-working/#findComment-1361898 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.