Jump to content


Photo

hidden php array


  • Please log in to reply
3 replies to this topic

#1 masgas

masgas
  • Members
  • PipPipPip
  • Advanced Member
  • 64 posts

Posted 10 July 2006 - 05:53 PM

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>HERE I WANT TO PRINT THE QUESTION</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>";
}
}


#2 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 10 July 2006 - 06:04 PM


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

        }
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#3 masgas

masgas
  • Members
  • PipPipPip
  • Advanced Member
  • 64 posts

Posted 10 July 2006 - 08:36 PM

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

#4 masgas

masgas
  • Members
  • PipPipPip
  • Advanced Member
  • 64 posts

Posted 10 July 2006 - 08:45 PM

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

Thanks a million...




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users