masgas Posted July 10, 2006 Share Posted July 10, 2006 hi!I've got a multiple choice test script in php. However, when it comes to correct the answers I'd like to show the original questions together with the option selected and the correct answer. The last two details I've already manage to get, but the original questions I can not make the script print them. I am not sure how to do that. The script is simple and it uses for and while loops. I wonder if anyone knows how to show the questions so people who make the test see it again when they are correcting it. Thanks in advance... )Here goes the script:<?phpfunction debug($var){ echo "<hr /><pre>"; print_r($var); echo "</pre><hr />";} $con=mysql_connect('',','') or die ('no se puede conectar con servidor'); mysql_select_db('examenes') or die ('no puede conectar con bd'); if (!isset($_POST['submit'])) { $sql = "SELECT * FROM question ORDER BY RAND() LIMIT 2"; $display = mysql_query ($sql) or die ('la consulta no se ha podido realizar'); if (0== mysql_num_rows($display)){ print "no hay preguntas en question"; } else { echo "<form method='post' action='{$_SERVER['$PHP_SELF']}'>"; echo "<center><table border='0' width='500px' background='/webex/img/background.jpg'>"; while ($row = mysql_fetch_array($display)) { $id = $row["id"]; $pregunta = $row["pregunta"]; $imagen = $row["imagen"]; $ans1 = $row["ans1"]; $ans2 = $row["ans2"]; $ans3 = $row["ans3"]; $ans4 = $row["ans4"]; $corans = $row["corans"]; echo "<tr><td width='100%' colspan='2'><br><table border=1 bordercolor='#e0e0e0' width=100%><tr><td bordercolor='#e0e0e0'><b>$pregunta<br></td></tr></table></b><br>"; if (0 < strlen ($row[imagen])){ print "<img src='$imagen'>"; } echo "</td></tr>"; echo "<tr> <td width='5%'><input type='radio' name='q[$id]' value='$ans1'></td><td>$ans1</td> </tr> <tr> <td><input type='radio' name='q[$id]'value='$ans2'></td><td>$ans2</td> </tr> <tr> <td><input type='radio' name='q[$id]'value='$ans3'></td><td>$ans3</td> </tr> <tr> <td><input type='radio' name='q[$id]'value='$ans4'></td><td>$ans4</td> </tr> <tr><td></td></td><td></tr>"; } $horainicio = time (); echo "</table>"; echo "<input type='hidden' name='horainicio' value='$horainicio'>"; echo "<input type='submit' value='Corrija su examen' name='submit'>"; echo "</form></center>"; } } else { //debug($_POST); print "<table border=1 bordercolor='#2279be' bgcolor='white'>"; print "<tr><td>Pregunta</td><td bgcolor='white' border='0'>Has elegido la opción</td> <td bgcolor='white' border='0'>la opción correcta era</td></tr>"; $score = 0; foreach ($_POST['q'] as $qid => $ans) { $res = mysql_query("SELECT corans FROM question WHERE id = '$qid'"); $corans = mysql_result ($res, 0); echo "<tr><td>[b][color=red]HERE I WANT TO PRINT THE QUESTION[/color][/b]</td><td bgcolor='white' border='0'>$ans</td><td bgcolor='white' border='0'>$corans</td></tr>"; if ($ans == $corans) { ++$score; } else { //echo "En la pregunta número has elegido la respuesta número $ans y debería haber elegido la $corans <br> <br>"; } } Quote Link to comment https://forums.phpfreaks.com/topic/14204-hidden-php-array/ Share on other sites More sharing options...
redarrow Posted July 10, 2006 Share Posted July 10, 2006 $res = mysql_query("SELECT corans FROM question WHERE id = '$qid'"); $corans = mysql_result ($res, 0); foreach ($_POST['q'] as $qid => $ans) { echo "<tr><td>$qid</td><td bgcolor='white' border='0'>$ans</td><td bgcolor='white' border='0'>$corans</td></tr>"; if ($ans == $corans) { ++$score; } Quote Link to comment https://forums.phpfreaks.com/topic/14204-hidden-php-array/#findComment-55690 Share on other sites More sharing options...
masgas Posted July 10, 2006 Author Share Posted July 10, 2006 aha! that got something going!!! though it prints the id number of the answered question and not the question itself!! mmmm I will work from here on, to see if I manage to get it right! thanks a lot! meanwhile if you see why it doesn't do what it is suppoused to do, I'll appreciate your comments... ;D Quote Link to comment https://forums.phpfreaks.com/topic/14204-hidden-php-array/#findComment-55809 Share on other sites More sharing options...
masgas Posted July 10, 2006 Author Share Posted July 10, 2006 ;) ;D :)heyyy Thanks I managed!!I qeueried again like this!!!$res1 = mysql_query("SELECT pregunta FROM question WHERE id = '$qid'");$preg = mysql_result ($res1, 0);and after I printed $pregThanks a million... Quote Link to comment https://forums.phpfreaks.com/topic/14204-hidden-php-array/#findComment-55815 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.