Doug Posted April 25, 2011 Share Posted April 25, 2011 Hello, I am trying to build a quiz with PHP and MySQL. I have successfully managed to build the database and display the questions from the database. I have also managed to get the radio button to individually map to each question (rather than seeing all questions with one possible answer). What I have been unable to do (for some days now) is to match the correct radio button with the answer (all say incorrect at the moment even if you answer correctly.) Any help would be greatly appreciated. Code below questions.php <?php require_once('connectvars1.php'); //Connect to the database $dbc = mysqli_connect(DB_Host, DB_User, DB_Password, DB_Name); $query = "SELECT * FROM questions"; $result = mysqli_query($dbc, $query); // if records are present if (mysqli_num_rows($result) > 0) { $row = mysqli_fetch_array($result); if (mysqli_num_rows($result) > 0) { // print answer list as check boxes while ($row = mysqli_fetch_array($result)) { $id = $row['autokey']; $quest = $row['q_text']; $option1 = $row['op1']; $option2 = $row['op2']; $option3 = $row['op3']; $option4 = $row['op4']; $answer= $row['answer']; echo "$quest"; ?> <br /> <input type= radio name= <?php echo "$id" ?> <value="A"/>A. <?php echo "$option1" ?><br /> <input type= radio name= <?php echo "$id" ?> <value="B"/>B. <?php echo "$option2" ?><br /> <input type= radio name= <?php echo "$id" ?> <value="C"/>C. <?php echo "$option3" ?><br /> <input type= radio name= <?php echo "$id" ?> <value="D"/>D. <?php echo "$option4" ?><br /> <br /> <?PHP } ?> <br /> <div align="center"><input type="submit" name="btn_submit" value="Submit Answers" /></div> <input type="hidden" name="submitted" value="TRUE" /> </form> <?php } echo '</form>'; } mysqli_close($dbc); ?> score.php <?php // Check answers if((isset($_POST['$id'])) && ($_POST['$id'] == "$answer")){ echo 'Your answer was correct for q1!'; }else{ echo 'Your answer was incorrect for q1.'; } ?> <br /> <?php if((isset($_POST['$id'])) && ($_POST['$id'] == "$answer")){ echo 'Your answer was correct for q2!'; }else{ echo 'Your answer was incorrect for q2.'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/234659-quiz-code-please-help/ Share on other sites More sharing options...
wildteen88 Posted April 25, 2011 Share Posted April 25, 2011 You'll want to name your radios buttons as question[$id], eg <input type="radio" name="question[<?php echo $id; ?>]" value="A"/>A. <?php echo $option1 ?><br /> <input type="radio" name="question[<?php echo $id; ?>]" value="B"/>B. <?php echo $option2 ?><br /> <input type="radio" name="question[<?php echo $id; ?>]" value="C"/>C. <?php echo $option3 ?><br /> <input type="radio" name="question[<?php echo $id; ?>]" value="D"/>D. <?php echo $option4 ?><br /> Now $_POST['question'] will be a multi-dimensional array containing all the users answers to the quiz Now in score.php you logic completly wrong here. Variables do not get passed from page to page. So the variable $answer will not exits in this page. What you will have to do in score.php is connect to your database. Run an SQL query and grab all the answers and place them in an array. // get all answers and add them to an array $query = 'SELECT autokey, answer FROM questions'; $result = mysql_query($query); while ($row = mysql_fetch_assoc($result)) { $question_id = $row['autokey']; $answer = $row['answer']; $question_answers[$question_id] = $answer; } Now we have the array of answers. We can now use a loop and compare the users answers. Like so // loop through the posted ansers and compare foreach($question_answers as $question_id => $question_answer) { if(isset($_POST['question'][$question_id]) { $user_answer = $_POST['question'][$question_id]; if($user_answer == $question_answer) { echo "<p>Your answer was correct for Q{$question_id}!<p>"; } else { echo "<p>Your answer was incorrect for Q{$question_id}.<p>"; } } else { echo "<p>You didn't give an anser for Q{$question_id}.<p>"; } } Quote Link to comment https://forums.phpfreaks.com/topic/234659-quiz-code-please-help/#findComment-1205883 Share on other sites More sharing options...
Doug Posted April 27, 2011 Author Share Posted April 27, 2011 Thanks very much for the help! There is a poblem still with score.php. I get the following error: Parse error: syntax error, unexpected '{' in line 28 Why would this be? I have tried swappiing the { around and deleting with the same result Help, as always, appreciated. Score.php <?php require_once('connectvars1.php'); //Connect to the database $dbc = mysqli_connect(DB_Host, DB_User, DB_Password, DB_Name); // get all answers and add them to an array $query = 'SELECT autokey, answer FROM questions'; $result = mysql_query($query); while ($row = mysql_fetch_assoc($result)) { $question_id = $row['autokey']; $answer = $row['answer']; $question_answers[$question_id] = $answer; } // loop through the posted ansers and compare foreach($question_answers as $question_id => $question_answer) { if(isset($_POST['question'][$question_id]) { $user_answer = $_POST['question'][$question_id]; if($user_answer == $question_answer) { echo "<p>Your answer was correct for Q{$question_id}!</p>"; } else { echo "<p>Your answer was incorrect for Q{$question_id}.</p>"; } } else { echo "<p>You didn't give an anser for Q{$question_id}.</p>"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/234659-quiz-code-please-help/#findComment-1206859 Share on other sites More sharing options...
kney Posted April 27, 2011 Share Posted April 27, 2011 I hope this helps... (Look in the code, I added a comment) Thanks very much for the help! There is a poblem still with score.php. I get the following error: Parse error: syntax error, unexpected '{' in line 28 Why would this be? I have tried swappiing the { around and deleting with the same result Help, as always, appreciated. Score.php <?php require_once('connectvars1.php'); //Connect to the database $dbc = mysqli_connect(DB_Host, DB_User, DB_Password, DB_Name); // get all answers and add them to an array $query = 'SELECT autokey, answer FROM questions'; $result = mysql_query($query); while ($row = mysql_fetch_assoc($result)) { $question_id = $row['autokey']; $answer = $row['answer']; $question_answers[$question_id] = $answer; } // loop through the posted ansers and compare foreach($question_answers as $question_id => $question_answer) { if(isset($_POST['question'][$question_id]) { $user_answer = $_POST['question'][$question_id]; if($user_answer == $question_answer) { echo "<p>Your answer was correct for Q{$question_id}!</p>"; } else { echo "<p>Your answer was incorrect for Q{$question_id}.</p>"; // you forgot to close here } // you forgot to close here } } else { echo "<p>You didn't give an anser for Q{$question_id}.</p>"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/234659-quiz-code-please-help/#findComment-1206884 Share on other sites More sharing options...
Doug Posted April 27, 2011 Author Share Posted April 27, 2011 This still gives the same error: Parse error: syntax error, unexpected '{ on line 20 in fact there are more } than { in this solution. I think the problem is in score.php but I will include the updated questions.php if this is involved. Help much appreciated <?php require_once('connectvars1.php'); //Connect to the database $dbc = mysqli_connect(DB_Host, DB_User, DB_Password, DB_Name); // get all answers and add them to an array $query = 'SELECT autokey, answer FROM questions'; $result = mysql_query($query); while ($row = mysql_fetch_assoc($result)) { $question_id = $row['autokey']; $answer = $row['answer']; $question_answers[$question_id] = $answer; } // loop through the posted ansers and compare foreach($question_answers as $question_id => $question_answer) { if(isset($_POST['question'][$question_id]) { $user_answer = $_POST['question'][$question_id]; if($user_answer == $question_answer) { echo "<p>Your answer was correct for Q{$question_id}!</p>"; } else { echo "<p>Your answer was incorrect for Q{$question_id}.</p>"; // you forgot to close here } // you forgot to close here } else { echo "<p>You didn't give an answer for Q{$question_id}.</p>"; } } ?> quiz.php <?php require_once('connectvars1.php'); //Connect to the database $dbc = mysqli_connect(DB_Host, DB_User, DB_Password, DB_Name); // get all answers and add them to an array $query = 'SELECT autokey, answer FROM questions'; $result = mysql_query($query); while ($row = mysql_fetch_assoc($result)) { $question_id = $row['autokey']; $answer = $row['answer']; $question_answers[$question_id] = $answer; } // loop through the posted ansers and compare foreach($question_answers as $question_id => $question_answer) { if(isset($_POST['question'][$question_id]) { $user_answer = $_POST['question'][$question_id]; if($user_answer == $question_answer) { echo "<p>Your answer was correct for Q{$question_id}!</p>"; } else { echo "<p>Your answer was incorrect for Q{$question_id}.</p>"; // you forgot to close here } // you forgot to close here } else { echo "<p>You didn't give an answer for Q{$question_id}.</p>"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/234659-quiz-code-please-help/#findComment-1207029 Share on other sites More sharing options...
wildteen88 Posted April 27, 2011 Share Posted April 27, 2011 You posted score.php twice there . Line 20 should read if(isset($_POST['question'][$question_id])) I only had one ) there should be of been two. Quote Link to comment https://forums.phpfreaks.com/topic/234659-quiz-code-please-help/#findComment-1207062 Share on other sites More sharing options...
Doug Posted April 29, 2011 Author Share Posted April 29, 2011 well spotted, thank you very much! It is working now. I had to make a few other changes that I worked out for myself: while ($row = mysql_fetch_assoc($result)) to while ($row = mysqli_fetch_assoc($result)). Sorry I have one other quetion for the code. THe first item in the database is not displayed. I'm sure there is a simple answer but am not sure what it is! Help, as before, much appreciated quiz.php <?php require_once('connectvars1.php'); //Connect to the database $dbc = mysqli_connect(DB_Host, DB_User, DB_Password, DB_Name); $query = "SELECT * FROM questions"; $result = mysqli_query($dbc, $query); // if records are present if (mysqli_num_rows($result) > 0) { $row = mysqli_fetch_array($result); if (mysqli_num_rows($result) > 0) { // print answer list as check boxes while ($row = mysqli_fetch_array($result)) { $id = $row['autokey']; $quest = $row['q_text']; $option1 = $row['op1']; $option2 = $row['op2']; $option3 = $row['op3']; $option4 = $row['op4']; $answer= $row['answer']; echo "$quest"; ?> <br /> <input type="radio" name="question[<?php echo $id; ?>]" value="A"/>A. <?php echo $option1 ?><br /> <input type="radio" name="question[<?php echo $id; ?>]" value="B"/>B. <?php echo $option2 ?><br /> <input type="radio" name="question[<?php echo $id; ?>]" value="C"/>C. <?php echo $option3 ?><br /> <input type="radio" name="question[<?php echo $id; ?>]" value="D"/>D. <?php echo $option4 ?><br /> <br /> <?PHP } ?> <br /> <div align="center"><input type="submit" name="btn_submit" value="Submit Answers" /></div> <input type="hidden" name="submitted" value="TRUE" /> </form> <?php } echo '</form>'; } mysqli_close($dbc); ?> Quote Link to comment https://forums.phpfreaks.com/topic/234659-quiz-code-please-help/#findComment-1208265 Share on other sites More sharing options...
wildteen88 Posted April 29, 2011 Share Posted April 29, 2011 Your issue is to do with this line $row = mysqli_fetch_array($result); Remove that line and the first question should now be displayed. Each time mysqli_fetch_assoc is called the next row in the result set will be returned. Quote Link to comment https://forums.phpfreaks.com/topic/234659-quiz-code-please-help/#findComment-1208341 Share on other sites More sharing options...
Doug Posted April 30, 2011 Author Share Posted April 30, 2011 That's Brilliant, Thanks so much for your help! I am continuing to learn! Quote Link to comment https://forums.phpfreaks.com/topic/234659-quiz-code-please-help/#findComment-1208593 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.