Jump to content

hidden php array


masgas

Recommended Posts

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:

<?php
function 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>";
}
}
Link to comment
Share on other sites


        $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;

        }
Link to comment
Share on other sites

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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.