Jump to content

PHP quiz code help..


jushiro

Recommended Posts

I've made a code here.. It can create a quiz.

It consist of a question and a 4 choices.. But my problem here is.

when i inserted only 2 values for the choices, there would still be 4 radio button(Choices).

Can someone help me. i want my code to be like : When user inputs 4 values. there would be 4 choices, but when user insert 2 values, there would be 2 values..

 

Here's my code for the form

 

<form name="formupload" method="post" action="setdate.php">
<div id="apDiv10"><input type="image" src="images/setdate.png" name="Image9" width="170" height="35" border="0" ></div>
</form><form name="formupload" method="post" action="valdeactiveass.php">
  <div id="apDiv8"><input type="image" src="images/deacass.png" name="Image9" width="170" height="35" border="0" ></div>
</form><form name="formupload" method="post" action="valactiveass.php">
<div id="apDiv7"><input type="image" src="images/activeass.png" name="Image9" width="170" height="35" border="0" ></div>
</form><form name="formupload" method="post" action="quiz_preview.php">
  <div id="apDiv3"><input type="image" src="images/preview.png" name="Image9" width="170" height="35" border="0" ></div>
</form>
<form name="form2" method="post" action="delques.php">
<div id="apDiv9"><input type="image" src="images/delques.png" name="Image8" width="170" height="35" border="0" ></div>
</form><form name="formupload" method="post" action="validatequiz.php">
  <input type="text"  style="position:absolute; top:160px; left:101px; width: 576px; height: 31px; font-size:10px;" name="question" id="question"
/>
  <input type="text"  style="position:absolute; top:244px; left:109px; width: 567px; height: 19px; font-size:10px;" name="c1" id="c1"
/>
  <input type="text"  style="position:absolute; top:297px; left:108px; width: 567px; height: 19px; font-size:10px;" name="c2" id="c2"
/>
  <input type="text"  style="position:absolute; top:350px; left:109px; width: 567px; height: 19px; font-size:10px;" name="c3" id="c3"
/><input type="text"  style="position:absolute; top:396px; left:110px; width: 567px; height: 19px; font-size:10px;" name="c4" id="c4"
/>
  <select style="position:absolute; top:463px; left:309px; width: 80px; height: 23px; font-size:10px;" name="answer" id="answer">
<option value="Select">Select</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select><div id="apDiv2"><input type="image" src="images/submit.png" name="Image8" width="170" height="35" border="0" ></div>

</form>

 

And this of the quiz maker..

<?php

if (isset($_POST['sent'])) {
for ($i=0;$i<count($questions);$i++) {
	if ($_POST['q2'.$i]=="w") {
		$score++;
	} else {

	}
}
$percent = number_format(($score/count($questions))*100,2,".",",");
echo("<br>".$score." out of ".count($questions)." (".$percent."% right)<br>\n");
} 


else {
echo("<form action=\"#\" method=\"post\">\n");
echo("<input type=\"hidden\" name=\"sent\">\n");
for ($i=0;$i<count($questions);$i++) {
	echo("<b>".$questions[$i][0]."</b><br><br>\n");
	if ($questions[$i][5]==1) {
		echo("<input type=\"radio\" name=\"q2".$i."\" value=\"c\"> ".$questions[$i][1]."<br>\n");
	} else {
		echo("<input type=\"radio\" name=\"q2".$i."\" value=\"w\"> ".$questions[$i][1]."<br>\n");
	}
	if ($questions[$i][5]==2) {
		echo("<input type=\"radio\" name=\"q2".$i."\" value=\"c\"> ".$questions[$i][2]."<br>\n");
	} else {
		echo("<input type=\"radio\" name=\"q2".$i."\" value=\"w\"> ".$questions[$i][2]."<br>\n");
	}
	if ($questions[$i][5]==3) {
		echo("<input type=\"radio\" name=\"q2".$i."\" value=\"c\"> ".$questions[$i][3]."<br>\n");
	} else {
		echo("<input type=\"radio\" name=\"q2".$i."\" value=\"w\"> ".$questions[$i][3]."<br>\n");
	}
	if ($questions[$i][5]==4) {
		echo("<input type=\"radio\" name=\"q2".$i."\" value=\"c\"> ".$questions[$i][4]."<br><br>\n");
	} else {
		echo("<input type=\"radio\" name=\"q2".$i."\" value=\"w\"> ".$questions[$i][4]."<br><br>\n");
	}
}
echo("<input type=\"submit\" value=\"Submit Exam\"></form>");
}
?>

Link to comment
https://forums.phpfreaks.com/topic/249049-php-quiz-code-help/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.