gerrydewar Posted March 8, 2006 Share Posted March 8, 2006 Hello everyone,Hope someone can help me out with my code here. I'm creating a multiple choice quiz and i have so far managed to generate random questions from my database and have them display on screen in a nice neat way. Each answer has a radio button so the user can select an answer. So as you can see from my script below i display the form before the submit button is pressed. As soon as the submit is pressed i would like the total number of correct answers to be shown. This is the bit of my script that is confusing me. I currently get an error message telling me that it doesn't like my 'mysql_fetch_array' just below the $score = 0 line. I think i'm quite close to solving this problem. Does anyone have any ideas? <body> <?php if($_SERVER['REQUEST_METHOD'] == 'POST'){ $score = 0; while($row = mysql_fetch_array($result,MYSQL_NUM)){ $answer = $row[2]; if ($guess == $answer) { $score++; }else{ $score = $score; } } echo '<p><b> you scored $score</b></p>'; }else{//Display the form. ?> <form name ="systemstest" action="<?php echo $_SERVER['PHP_SELF'];?>" method="post"> <div align="center"><input type="submit" value="check score" name="submitted"></div> <?php //Connect to db. require_once ('../mysql_connect.php'); //Select 5 random questions from database $query = "SELECT question_id, question, answer, choice1, choice2, choice3 FROM questions WHERE subject_id = 1 ORDER BY RAND() LIMIT 5"; $result = @mysql_query ($query); //Run query if($result) { echo '<table align="left" cellspacing="10" cellpadding="10">'; while ($row=mysql_fetch_array($result,MYSQL_NUM)){ $id = $row[0]; $questions = $row[1]; $answer = $row[2]; $opt1 = $row[3]; $opt2 = $row[4]; $opt3 = $row[5]; echo "<tr><td align=\"left\"><li>$questions</td></tr> <tr> <td align=\"left\"><input type=\"radio\" name=\"guess\"value=\"$opt1\"/>$opt1</td></tr> <tr> <td align=\"left\"><input type=\"radio\" name=\"guess\"value=\"$opt2\"/>$opt2</td></tr> <tr> <td align=\"left\"><input type=\"radio\" name=\"guess\"value=\"$opt3\"/>$opt3</td></tr> \n"; } echo '</table>'; //echo '<table>'; }else{ echo'<p>Test failed</p>'; } mysql_close(); } ?></form> </body> Quote Link to comment Share on other sites More sharing options...
keeB Posted March 8, 2006 Share Posted March 8, 2006 Just looking code over, what's the MYSQL_NUM for?also, can you give the error, please? Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted March 8, 2006 Share Posted March 8, 2006 When you press submit, your script is started again, so you have to do all of the preliminary database stuff again.Also instead of[code]<?php if($_SERVER['REQUEST_METHOD'] == 'POST') ?>[/code]you probably want to use[code]<?php if (isset($_POST['submitted'])) ?>[/code]Ken Quote Link to comment Share on other sites More sharing options...
AV1611 Posted March 8, 2006 Share Posted March 8, 2006 I cut some fat... See my other remarks at the end...[code]<body> <?php if($_SERVER['REQUEST_METHOD'] == 'POST'){ $score = 0; while($row = mysql_fetch_array($result,MYSQL_NUM)){ $answer = $row[2]; if ($guess == $answer) { $score++; } } } echo '<p><b> you scored '.$score.'</b></p>'; }<snip>[/code]I don't use PHP_SELF so I don't really know the answer for sure, but I don't know if you can cycle through a result twice... I would have made the answers post hidden with the responces in the form... That seems easier to me... Quote Link to comment Share on other sites More sharing options...
gerrydewar Posted March 8, 2006 Author Share Posted March 8, 2006 [!--quoteo(post=353019:date=Mar 8 2006, 09:42 PM:name=keeB)--][div class=\'quotetop\']QUOTE(keeB @ Mar 8 2006, 09:42 PM) [snapback]353019[/snapback][/div][div class=\'quotemain\'][!--quotec--]Just looking code over, what's the MYSQL_NUM for?also, can you give the error, please?[/quote]I use the MYSQL_NUM for the array as my database info gets stuck into an array. The error i am getting is Warning: mysql_fetch_array():supplied argument is not a valid MySQL result resource in c:\.....on line .... Quote Link to comment Share on other sites More sharing options...
gerrydewar Posted March 8, 2006 Author Share Posted March 8, 2006 [!--quoteo(post=353020:date=Mar 8 2006, 09:42 PM:name=kenrbnsn)--][div class=\'quotetop\']QUOTE(kenrbnsn @ Mar 8 2006, 09:42 PM) [snapback]353020[/snapback][/div][div class=\'quotemain\'][!--quotec--]When you press submit, your script is started again, so you have to do all of the preliminary database stuff again.Also instead of[code]<?php if($_SERVER['REQUEST_METHOD'] == 'POST') ?>[/code]you probably want to use[code]<?php if (isset($_POST['submitted'])) ?>[/code]Ken[/quote]I'll give that a go. How much of the preliminary database stuff? Just//Connect to db.require_once ('../mysql_connect.php');or anymore? Quote Link to comment Share on other sites More sharing options...
AV1611 Posted March 9, 2006 Share Posted March 9, 2006 Again, if you put the answer in <input type=hidden> along with the responces, you wouldn't need to run through it again... Quote Link to comment Share on other sites More sharing options...
gerrydewar Posted March 9, 2006 Author Share Posted March 9, 2006 [!--quoteo(post=353108:date=Mar 9 2006, 02:07 AM:name=AV1611)--][div class=\'quotetop\']QUOTE(AV1611 @ Mar 9 2006, 02:07 AM) [snapback]353108[/snapback][/div][div class=\'quotemain\'][!--quotec--]Again, if you put the answer in <input type=hidden> along with the responces, you wouldn't need to run through it again...[/quote]Here is what i have now. It gives an answer of 0 all the time so far. All i want to be able to do is compare the selected radio button value with the correct answer. If it is correct then add one to total. How hard can that be? Bloody hard in my case!!!! <body> <form name ="systemstest" action="<?php echo $_SERVER['PHP_SELF'];?>" method="post"> <div align="center"><input type="submit" value="check score" name="submitted"></div> <?php //Connect to db. require_once ('../mysql_connect.php'); //Select 5 random questions from database $query = "SELECT question_id, question, answer, choice1, choice2, choice3 FROM questions WHERE subject_id = 1 ORDER BY RAND() LIMIT 5"; $result = @mysql_query ($query); //Run query if(!isset($_POST['submitted'])){ //display the form if($result) { echo '<table align="left" cellspacing="10" cellpadding="10">'; while ($row=mysql_fetch_array($result,MYSQL_NUM)){ $id = $row[0]; $questions = $row[1]; $answer = $row[2]; $opt1 = $row[3]; $opt2 = $row[4]; $opt3 = $row[5]; echo "<tr><td align=\"left\"><li>$questions</td></tr> <tr> <td align=\"left\"><input type=\"radio\" name=\"$id\" value=\"$opt1\"/>$opt1</td></tr> <tr> <td align=\"left\"><input type=\"radio\" name=\"$id\" value=\"$opt2\"/>$opt2</td></tr> <tr> <td align=\"left\"><input type=\"radio\" name=\"$id\" value=\"$opt3\"/>$opt3</td></tr> <tr> <td align=\"left\"><input type=\"hidden\"name=\"rightanswer\" value=\"$answer\"/></td></tr> \n"; } echo '</table>'; echo '</form>'; }else{ echo'<p>Test failed</p>'; } }else{ $score=0; if($_POST['$id']==$_POST['rightanswer']){ $score++; } echo 'you scored '.$score.''; mysql_close(); } ?> </body> </html> Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted March 9, 2006 Share Posted March 9, 2006 Are you trying to make the names of the radiobuttons the same as your the contents of your varible "$ID"? If so, your script is wrong. Try this instead:[code]<?phpecho '<tr><td align="left"><li>' . $questions . '</td></tr><tr> <td align="left"><input type="radio" name="choice[' . $id . ']" value="' . $opt1 .'"/>' . $opt1 . '</td></tr><tr> <td align="left"><input type="radio" name="choice[' . $id . ']" value="' . $opt2 .'"/>' . $opt2 . '</td></tr><tr> <td align="left"><input type="radio" name="choice[' . $id . ']" value="' . $opt3 .'"/>' . $opt3 . '</td></tr><tr> <td align="left"><input type="hidden" name="rightanswer[' . $id . ']" value="' . $answer . '"/></td></tr>' . "\n";}?>[/code]Then in your processing code:[code]<?php$score = 0;foreach($_POST['choice'] as $id => $ans) if($_POST['rightanswer'][$id] == $ans) $score++;?>[/code]The way you have it scripted all of your radio buttons were named "$id" so if you had more than one question only the last choices would be returned. The same with the right answer.Ken Quote Link to comment Share on other sites More sharing options...
gerrydewar Posted March 10, 2006 Author Share Posted March 10, 2006 Worked a treat, thanks Ken. Quote Link to comment 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.