how to do a questionnaire result?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
lucibalaj
Forum Newbie
Posts: 3
Joined: Sat Jun 17, 2006 4:57 am

how to do a questionnaire result?

Post by lucibalaj »

Hello,

I want to build a simple Questionnaire: 10 questions with 3 possible answers(1, 2, or all may be true) for each question. When a user clicks submit I want him to have the results of the Questionnaire (something like, you have answered correctly at 3 questions: no2, no7, no9). Can anybody help me? Thank you, Lucian.
User avatar
aerodromoi
Forum Contributor
Posts: 230
Joined: Sun May 07, 2006 5:21 am

Re: how to do a questionnaire result?

Post by aerodromoi »

lucibalaj wrote:Hello,

I want to build a simple Questionnaire: 10 questions with 3 possible answers(1, 2, or all may be true) for each question. When a user clicks submit I want him to have the results of the Questionnaire (something like, you have answered correctly at 3 questions: no2, no7, no9). Can anybody help me? Thank you, Lucian.
Here's a basic example.

Code: Select all

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <title>.:survey:.</title>
	<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
</head>
<body>
<?php
if(empty($_POST)){
?>
<form name="myform" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<p><input type="radio" name="group1" value="1"> option 1<br />
   <input type="radio" name="group1" value="2"> option 2<br />
   <input type="radio" name="group1" value="3"> option 3</p>
<input type="submit" value="send">
</form>
<?php
}else{
 if (!empty($_POST['group1']) && eregi("^[0-9]$", $_POST['group1'])){
   echo "You've chosen option ".$_POST['group1']."<br />";
   if ($_POST['group1']=="1"){ echo "This is correct!";}
   else {echo "Sorry, wrong answer!";}
 }
 else{
   echo "Error!";
 }
}
?>
</body>
</html>
Greetings,
aerodromoi
lucibalaj
Forum Newbie
Posts: 3
Joined: Sat Jun 17, 2006 4:57 am

PHP& MySQL solution?

Post by lucibalaj »

Hello Marc,

Thank you for your fast reply.
Sorry, I forgot to add that I want to implement this with PHP and MySQL, that is I want that the questions and the answers to be fed into the page from a MySQL database. Do you know any solution for this? Or a suggestion? Thank you and good luck,
Lucian.
User avatar
aerodromoi
Forum Contributor
Posts: 230
Joined: Sun May 07, 2006 5:21 am

Post by aerodromoi »

Here's a modified version using a two-dimensional array:

Code: Select all

<?php
$q_a_array = array(
				array("question1","description 1","option1.1","option1.1","option1.2","option1.3"), 
				array("question2","description 2","option2.4","option2.1","option2.2","option2.3","option2.4"),
				array("question3","description 3","option3.3","option3.1","option3.2","option3.3"),
			);
// syntax: Name of group, description/question, correct answer, possible answers
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <title>.:survey:.</title>
	<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
</head>
<body>
<?php
if(empty($_POST)){
  echo "<form name=\"myform\" action=\"".$_SERVER['PHP_SELF']."\" method=\"post\">\n";
  for ($i=0;$i<count($q_a_array);$i++){
    echo "<p>\n";
    echo "<b>".$q_a_array[$i][0]."</b><br />\n";
    for ($m=0;$m<count($q_a_array[$i])-3;$m++){
      echo "<input type=\"radio\" name=\"".$q_a_array[$i][0]."\" value=\"".($m+3)."\"> ".$q_a_array[$i][($m+3)];
      if ($i != count($q_a_array)-4) {echo "<br />\n";}
    }
    echo "</p>\n";
  }
  echo "<input type=\"submit\" value=\"send\">\n";
  echo "</form>";
}
else{
 echo "<h2>result:</h2>\n";
 for ($i=0;$i<count($q_a_array);$i++){
   $answer = $_POST[$q_a_array[$i][0]];   
   echo "<p>\n";
   echo "<b>".$q_a_array[$i][0]."</b><br />\n";
   if (!empty($answer) && eregi("^[0-9]$", $answer)){
     echo "You've chosen option ".($answer-2)."<br />";
     if ($q_a_array[$i][$answer] == $q_a_array[$i][2]){ echo "This is correct!";}
     else {echo "Sorry, wrong answer!";}
   }
   else{
     echo "Error!";
   }
   echo "</p>\n";
 }
}
?>
</body>
</html>
As I don't know your database layout, it wouldn't make any sense to incorporate this in the script.
The array approach should get you started, though.

Marc
lucibalaj
Forum Newbie
Posts: 3
Joined: Sat Jun 17, 2006 4:57 am

indeed

Post by lucibalaj »

Hi Marc,

Thanks for the solution. I've tried locally on my PC and this is what I want to have. However, how can I do it by using MySQL? (
I've created a database like this:

`id` int(11) NOT NULL auto_increment,
`question` text NOT NULL, //the text of the question
`answer_1` text, // the answer 1
`answer_2` text, // the answer 2
`answer_3` text, // the answer 3
`right_value` varchar(200) default NULL, //the right answer
`picture` varchar(200) default NULL, // a picture of the question
PRIMARY KEY (`id`)
) TYPE=MyISAM AUTO_INCREMENT=3 ;

Once again, thank you and have a nice week-end!
Luci.
Post Reply