i'm try creating a quiz script with data from mysql, but I'm facing problem when I try retrieve a data from checkbox and mysql ok here the code:
<?php
//function display soal
function soal()
{
global $aid;
$qs = "SELECT sid,soal,pilihan1,pilihan2,pilihan3,pilihan4,pilihan5,jawaban FROM soal";
$rs = mysql_query($qs) or die (mysql_error());
echo "<form action='' method='POST'>";
$count = 1;
// buat simpen id ke array
$aid = array();
while ($row = mysql_fetch_array($rs))
{
$id = $row['sid'];
$p1 = $row['pilihan1'];
$p2 = $row['pilihan2'];
$p3 = $row['pilihan3'];
$p4 = $row['pilihan4'];
$p5 = $row['pilihan5'];
$add = '[]';
// generate all soal
echo '<ul>'.$count . " . " . $row['soal'] . "<br>";
echo "<input type='hidden' name='$id' value='$id'>";
echo "<input type='radio' name='$id$add' value='$p1'>$p1<BR>";
echo "<input type='radio' name='$id$add' value='$p2'>$p2<BR>";
echo "<input type='radio' name='$id$add' value='$p3'>$p3<BR>";
echo "<input type='radio' name='$id$add' value='$p4'>$p4<BR>";
echo "<input type='radio' name='$id$add' value='$p5'>$p5<BR>";
echo "</ul></ul>";
array_push($aid, $id);
$count++;
}
// button
echo '<ul><ul>';
echo "<input type='submit' name='jawab' value='jawab'>";
echo "<input type='reset' name='reset' value='clear'>";
echo "</ul></ul>";
echo '</form>';
}
//call
soal();
//calculation
if (isset($_POST['jawab']) && ($_POST['jawab']))
{
$qj = "SELECT sid,jawaban FROM soal";
$rj = mysql_query($qj) or die(mysql_error());
foreach ($aid as $a)
{
$q = $_POST[$a];
for (
;
list(,$val) = each( $q ) ,
list(,$jwb) = mysql_fetch_array($rj)
;
)
{
echo "data from mysql : <font color='red'>" . $jwb . "</font><br />\n";
echo "result from question/checkbox : <font color='blue'>" . $val . "</font><br />\n";
}
}
}
?>
the $val just shown one data not all, but when I put the $val under the $jwb the $jwb just shown 3 data, i've try add while but it add multiple data.
data from mysql : [B]pilihan dua[/B]
result from question/checkbox : [I]pilihan satu[/I]
data from mysql : [B]pilihan tiga[/B]
result from question/checkbox :
data from mysql : [B]pilihan empat[/B]
result from question/checkbox :
data from mysql : [B]pilihan dua[/B]
result from question/checkbox :
data from mysql : [B]pilihan tiga[/B]
result from question/checkbox :
data from mysql : [B]pilihan satu[/B]
result from question/checkbox :
result : when $val under the $jwb
data from mysql : [B]pilihan dua[/B]
result from question/checkbox : [I]pilihan satu[/I]
data from mysql : [B]pilihan empat[/B]
result from question/checkbox : [B]pilihan satu[/B]
data from mysql : [B]pilihan tiga[/B]
result from question/checkbox : [B]pilihan satu[/B]
data from mysql :
result from question/checkbox : [B]pilihan satu[/B]
data from mysql :
result from question/checkbox : [B]pilihan satu[/B]
data from mysql :
result from question/checkbox : [B]pilihan satu[/B]
regards.
thanks you.