Robię sobie prosty skrypt do obsługi ankiety/quizu.
Wszystko w sumie już mam, pozostała mi tylko zmiana koloru odpowiedzi po zatwierdzeniu danych.
Chciałbym, żeby dla złych odpowiedzi kolor label ustawił się na czerwony, a dla dobrych na zielony.
Wiem, że trzeba ustawić "style=color:..." ale nie wiem za bardzo jak się dobrać do danej label. Poniżej jest
kod, może ktoś będzie miał jakiś pomysł?
Kod
<?php
// Liczba możliwych odpowiedzi na 1 pytanie
define('N', "3");
// Pytania
$questions = array ("1st question", "2nd question", "3rd question", "4th question", "5th question", "6th question", "7th question", "8th question", "9th question", "10th question");
// Dobre odpowiedzi
$answers = array ("00", "10", "20", "30", "40", "50", "60", "70", "80", "90");
if (isset($_POST['check'])) {
$all = true;
$score = 0;
for ($i = 0; $i < count($questions); $i++) {
$selected_radio = $_POST[$i];
if (!isset($selected_radio)) {
$all = false;
break;
}
else {
if ($selected_radio == $answers[$i])
$score++;
}
}
if ($all) {
$final_score = $score / count($questions) *100;
echo "Twój wynik to ".$final_score."%";
}
else
echo "Zaznacz wszystkie odpowiedzi";
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<form action="index.php" name="" method="post">
<ul>
<?php
for ($i = 0; $i < count($questions); $i++) {
echo "<li><label>$questions[$i]</label>
";
for ($j = 0; $j < N; $j++) {
echo "<input type=\"radio\" name=\"$i\" id=\"$i$j\" value=\"$i$j\"";
if (isset($_POST[$i])) {
if ($_POST[$i] == "$i$j")
echo "checked=\"checked\" />";
}
echo "<label for=\"$i$j\">$i$j</label>
";
}
echo "</li>";
}
?>
</ul>
<input type="submit" value="Sprawdź" name="check" />
</form>
</body>
</html>
// Liczba możliwych odpowiedzi na 1 pytanie
define('N', "3");
// Pytania
$questions = array ("1st question", "2nd question", "3rd question", "4th question", "5th question", "6th question", "7th question", "8th question", "9th question", "10th question");
// Dobre odpowiedzi
$answers = array ("00", "10", "20", "30", "40", "50", "60", "70", "80", "90");
if (isset($_POST['check'])) {
$all = true;
$score = 0;
for ($i = 0; $i < count($questions); $i++) {
$selected_radio = $_POST[$i];
if (!isset($selected_radio)) {
$all = false;
break;
}
else {
if ($selected_radio == $answers[$i])
$score++;
}
}
if ($all) {
$final_score = $score / count($questions) *100;
echo "Twój wynik to ".$final_score."%";
}
else
echo "Zaznacz wszystkie odpowiedzi";
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<form action="index.php" name="" method="post">
<ul>
<?php
for ($i = 0; $i < count($questions); $i++) {
echo "<li><label>$questions[$i]</label>
";
for ($j = 0; $j < N; $j++) {
echo "<input type=\"radio\" name=\"$i\" id=\"$i$j\" value=\"$i$j\"";
if (isset($_POST[$i])) {
if ($_POST[$i] == "$i$j")
echo "checked=\"checked\" />";
}
echo "<label for=\"$i$j\">$i$j</label>
";
}
echo "</li>";
}
?>
</ul>
<input type="submit" value="Sprawdź" name="check" />
</form>
</body>
</html>