questo è il codice che uso
la prima query (con Rand) mi richiama 5 items
Codice PHP:
if ($tema=$_GET['tema'])
{
echo "<h1>questionario di ". $tema . "</h1>";
echo "<form method=post action=$_SERVER[PHP_SELF]>";
echo "<table>";
$display = "SELECT * FROM quiz where tema ='$tema' order by rand() limit 5";
$display = mysql_query($display) or die (mysql_error());
$i=1;
while($row =mysql_fetch_array($display))
{
$id = $row["id"];
$question = $row["question"];
$opt1 = $row["opt1"];
$opt2 = $row["opt2"];
$opt3 = $row["opt3"];
$answer = $row["answer"];
$tema =$row['tema'];
echo "<table>";
echo "<tr>";
echo "<input type=hidden name=tema value=\"$tema\">";
echo "</tr>";
echo "<tr>";
echo "<td colspan=3>
[b]$i-$question[/b]
</td>";
echo "</tr>";
$i++;
echo "<tr>";
echo "[list=1]";
echo "<td class='td'>[*]$opt1</td>";
echo "<td><input type=radio name=q$id value=\"$opt1\"></td>";
echo "</tr>";
echo "<tr>";
echo "<td class='td'>[*]$opt2</td>";
echo "<td> <input type=radio name=q$id value=\"$opt2\"></td>";
echo "</tr>";
echo "<tr>";
echo "<td class='td'>[*]$opt3</td>";
echo "<td><input type=radio name=q$id value=\"$opt3\"></td>";
echo "[/list]";
echo "</tr>";
echo "</table>";
}
echo "
";
echo "<input type='submit' value='verifica il risultato' name='submit'>";
echo "</form>";
echo "<p class='p'>[url='temaquiz.php']torma al menu[/url] | ";
echo "[url='esporta.php?tipo=word&tema=$tema']Esporta in Word[/url]";
}
dopo aver fatto la scelta delle risposte vado avanti con il submit
il confronto delle risposte le ho su tutta la tabella (che può avere anche 50 items, ad esempio)con il codice che segue,
mentre io vorrei solo per le domande scelte dal rand() della precedente query
il codice successivo è il seguente:
Codice PHP:
elseif($submit)
{
$tema=$_POST['tema'];
$score = 0;
$display = "SELECT * FROM quiz where tema ='$tema'";
$display = mysql_query($display) or die (mysql_error()); $total = mysql_num_rows($display);
while ($result = mysql_fetch_array($display))
{
$answer = $result["answer"];
$q = $result["q"];
if ($$q == $answer) {
$score++;
}
}
$perc = ($score *100)/$total;
$perc = round($perc, 2);
echo "<p align=center>[b]il punteggio raggiunto e' " . $score . " su un totale di " . $total . ". La percentuale e' ". $perc . "%[/b]</p>";
echo "
";
i dati della tabella:
CREATE TABLE quiz (
id tinyint(4) NOT NULL auto_increment,
q text NOT NULL,
question text NOT NULL,
opt1 text NOT NULL,
opt2 text NOT NULL,
opt3 text NOT NULL,
answer text NOT NULL,
PRIMARY KEY (id)
) TYPE=MyISAM;
spero che sia chiaro altrimenti chiedi