Hi all, Can anyone offer assistance with the following: I am trying to obtain a score from a users radio button selection on a simple quiz. I used the print_r($_POST); to test if the form is passing the correct info and that seems to be fine. But the score calculation section falls apart - keeps saying you scored 0 out of 3 (even when the right answers have been passed by the form). Any ideas? Thanks Steve <?php include("contentdb.php"); $display = mysql_query("SELECT * FROM $table ORDER BY id",$db); if (!$_POST['submit']) { echo "<form method=post action=$PHP_SELF>"; echo "<table border=0>"; while ($row = mysql_fetch_array($display)) { $id = $row["id"]; $question = $row["question"]; $opt1 = $row["opt1"]; $opt2 = $row["opt2"]; $opt3 = $row["opt3"]; $answer = $row["answer"]; echo "<tr><td colspan=3><br><b>$question</b></td></tr>"; echo "<tr><td>$opt1 <input type=radio name=q$id value=\"$opt1\"></td><td>$opt2 <input type=radio name=q$id value=\"$opt2\"></td><td>$opt3 <input type=radio name=q$id value=\"$opt3\"></td></tr>"; } echo "</table>"; echo "<input type='submit' value='See how you did' name='submit'>"; echo "</form>"; } elseif ($_POST['submit']) { $score = 0; $total = mysql_num_rows($display); while ($result = mysql_fetch_array($display)) { $answer = $result["answer"]; $q = $result["q"]; if ($$q == $answer) { $score++; } } echo "<p align=center><b>You scored $score out of $total</b></p>"; echo "<p>"; } ?> PHP:
<?php include("contentdb.php"); $display = mysql_query("SELECT * FROM $table ORDER BY id",$db); if (!$_POST['submit']) { echo "<form method=post action=$PHP_SELF>"; echo "<table border=0>"; while ($row = mysql_fetch_array($display)) { $id = $row["id"]; $question = $row["question"]; $opt1 = $row["opt1"]; $opt2 = $row["opt2"]; $opt3 = $row["opt3"]; echo " <tr><td colspan=3><br><b>$question</b></td></tr>"; echo "<tr><td>$opt1 <input type=radio name=q$id value=\"$opt1\"></td><td>$opt2 <input type=radio name=q$id value=\"$opt2\"></td><td>$opt3 <input type=radio name=q$id value=\"$opt3\"></td></tr>"; } echo "</table>"; echo "<input type='submit' value='See how you did' name='submit'>"; echo "</form>"; } elseif ($_POST['submit']) { $score = 0; $total = mysql_numrows($display); while ($result = mysql_fetch_array($display)) { $answer = $result["answer"]; $id = $result["id"]; if ($_POST[q$id] == $answer) { $score++; } } echo "<p align=center><b>You scored $score out of $total</b></p>"; echo "<p>"; } ?> Code (markup):
Thank you for your answer. I tried implementing your code, but the scoring section still seems to contain a problem. It won't output anything to the browser ... Anything else you could suggest? Thanks anyway Steve
On line 44: if ($$q == $answer) { is that double dollar sign a typo? That shouldn't be there I would guess, and could very well be the problem...
Ahh... I'm an idiot. Sorry. I would say that PinoyIto was on the right track, but instead of: if ($_POST[q$id] == $answer) { I'd do something more like: if ($_POST[ 'q' . $id] == $answer) { Anyway, good luck with it all...
Hmm... I have a small suggestion Try quoting the name here and in the other checkboxes <input type=radio name=q$id value=\"$opt1\"> to <input type=\"radio\" name=\"q$id\" value=\"$opt3\"> I cant find anything wrong with the script now. But I will keep looking. Thomas
Hi PinoyIto, Here is the url to my small test quix script sample: http://dev.imaging4.co.nz/athleticsQuiz/pages/admin/quizv1.0/quiz1.php I have included a print_r(['$_POST']) at the end to see what info the form was passing itself. That's why the array info is displayed .. Cheers Steve
I tried this script in my server and it works <?php include("contentdb.php"); $display = mysql_query("SELECT * FROM $table ORDER BY id",$db); if (!$_POST['submit']) { echo "<form method=post action=$PHP_SELF>"; echo "<table border=0>"; while ($row = mysql_fetch_array($display)) { $id = $row["id"]; $question = $row["question"]; $opt1 = $row["opt1"]; $opt2 = $row["opt2"]; $opt3 = $row["opt3"]; $answer = $row["answer"]; echo "<tr><td colspan=3><br><b>$question</b></td></tr>"; echo "<tr><td>$opt1 <input type=radio name='q$id' value=\"$opt1\"></td> <td>$opt2 <input type=radio name='q$id' value=\"$opt2\"></td> <td>$opt3 <input type=radio name='q$id' value=\"$opt3\"></td></tr>"; } echo "</table>"; echo "<input type='submit' value='See how you did' name='submit'>"; echo "</form>"; } elseif ($_POST['submit']) { $score = 0; $total = mysql_num_rows($display); while ($result = mysql_fetch_array($display)) { $answer = $result[answer]; $q = "q$result[id]"; $q = trim($q); if ($_POST[$q] == $answer) { $score++; } } echo "<p align=center><b>You scored $score out of $total</b></p>"; echo "<p>"; } ?> Code (markup):
if I use this: $display = mysql_query("SELECT * FROM $table ORDER BY RAND() LIMIT 4",$db); instead of the following: $display = mysql_query("SELECT * FROM $table ORDER BY id",$db); then is not come correct answer .. But why Please give answer to me......
Thanks for your good & effective script.. I use this script and its works good... here is my links where i used that script.. varsitycampus.com/bcs/test.php Thanks
I'm delighted to say the last script entered worked perfectly for me, once I changed my background table to the structure the author used. But there was much in the script that confused me, and I don't like simply copying others work without understanding it implicitly. Don't want to post my questions just yet, since the author of the script may no longer be taking questions for what is a fairly old script. I'll wait to see if there is anyone out there who had contributed to this thread previously and is still picking up their e-mails to the thread before posting further questions. Thanks.