Creating a quiz

Hi,
I am working a quiz containing 6 questions and every 6 questions have 5 answers to choose from and input type is radio button, my problem is that a user can check any combination of answers and if i do a manual conditional statement and creating them, it would be like 35 something if statements.which is a nuts situation. is there another way?

If I understand you correctly you have 6 questions with 5 possible answers to the questions - I assume only one answer will be correct so that you only need one if statement for each question. If the answer is correct else the answer was wrong.

To avoid if() you could use switch which may be a bit shorter.

Imagine nearly 70 possible loop or switch statements based on what person answer that statement get pulled, i want to avoid making loops 70 times, this is my code, it got a 3-4 loops, in each loop you can see the terms changes as it pulls answer from different table (terms), i want a smarter way.instead of repeating loop 70 times.

Might I suggest you make one query and build a multidimensional array containing each category and series of questions. You then check if a key exists and if not show questions. If there is a key you check if key exists at the next level and so on. The POST results could be saved to session at each stage to be used at final outcome of the quiz.

but how, i can make pseudo code but not logical, as i m not good in php.

Here’s a rough example

<?php
session_start();

$questions = array(
	"question1" => array(
		"answer1a",
		"answer1b",
		"answer1c",
		"answer1d",
		"answer1e"),
	"question2" => array(
		"answer2a",
		"answer2b",
		"answer2c",
		"answer2d",
		"answer2e"),
	"question3" => array(
		"answer3a",
		"answer3b",
		"answer3c",
		"answer3d",
		"answer3e"),
	"question4" => array(
		"answer4a",
		"answer4b",
		"answer4c",
		"answer4d",
		"answer4e"),
	"question5" => array(
		"answer5a",
		"answer5b",
		"answer5c",
		"answer5d",
		"answer5e"),
	"question6" => array(
		"answer6a",
		"answer6b",
		"answer6c",
		"answer6d",
		"answer6e")
);
							
///////////////////////////////////////
/////Set answer session if not set/////
///////////////////////////////////////
if (!isset($_SESSION['answers'])){$_SESSION['answers'] = array();}

///////////////////////////////////////
///////////// Processing //////////////
///////////////////////////////////////
if($_SERVER['REQUEST_METHOD'] == 'POST'){
	foreach($_POST as $question => $a){
		//Only use question keys//
		if($question != "submit" && $question != "showresult"){
			$_SESSION['answers'][$question] = $a;
		}
	}
}

///Build answer display or could be used to insert to DB

if(isset($_POST['showresult'])){
	$display = "";
	foreach($_SESSION['answers'] as $question => $a){
		$display .= "$question" . ":" . "$a <br />\\r";
	}
}

?>
<html>
<body>
<?php
echo '<form action="" method="post">';
	$q_num = count($_SESSION['answers']) + 1;	
	$currentQ = "question" . $q_num;
	
	//check to see if question is available//
	if(array_key_exists($currentQ,$questions)){
	
		//check that session answers is set and question key is not in array
		if(isset($_SESSION['answers']) && !array_key_exists($currentQ,$_SESSION['answers'])){
			foreach($questions[$currentQ] as $k => $qtext){
				echo "<input type=\\"radio\\" name=\\"$currentQ\\" value=\\"$k\\" />$qtext<br />\\r";
			}
		}
		echo '<input type="submit" name="submit" value="Submit" />';
	}else{
		echo '<input type="submit" name="showresult" value="Show Result" />';	
	}
echo '</form>';

if(isset($display)){ echo $display;}
?>
</body>
</html>	

Now my task is to understand it.

Here’s a slightly different version that allows more flexibility for questions but will require two arrays with matching numeric keys. Although you can’t see these keys in the questions array they are there (0,1,2 etc) just like the answer array.

<?php
session_start();

$questions = array(	
	"This is question one",
	"This is question two",
	"This is question three",
	"This is question four",
	"This is question five",
	"This is question six"
);
$answers = array(
	
	0 => array(
		"answer1a",
		"answer1b",
		"answer1c",
		"answer1d",
		"answer1e"),
	1 => array(
		"answer2a",
		"answer2b",
		"answer2c",
		"answer2d",
		"answer2e"),
	2 => array(
		"answer3a",
		"answer3b",
		"answer3c",
		"answer3d",
		"answer3e"),
	3 => array(
		"answer4a",
		"answer4b",
		"answer4c",
		"answer4d",
		"answer4e"),
	4 => array(
		"answer5a",
		"answer5b",
		"answer5c",
		"answer5d",
		"answer5e"),
	5 => array(
		"answer6a",
		"answer6b",
		"answer6c",
		"answer6d",
		"answer6e")
);	
							
///////////////////////////////////////
/////// Clear session answers /////////
///////////////////////////////////////
if(isset($_POST['clearresults'])){
	unset($_SESSION['answers']);
}
							
///////////////////////////////////////
/////Set answer session if not set/////
///////////////////////////////////////
if (!isset($_SESSION['answers'])){$_SESSION['answers'] = array();}

///////////////////////////////////////
///////////// Processing //////////////
///////////////////////////////////////
if($_SERVER['REQUEST_METHOD'] == 'POST'){
	foreach($_POST as $question => $a){
		//Only use question keys//
		if($question != "submit" && $question != "showresult" && $question != "clearresults"){
			$_SESSION['answers'][$question] = $a;
		}
	}
}

///Build answer display or could be used to insert to DB

if(isset($_POST['showresult'])){
	$display = "";
	foreach($_SESSION['answers'] as $question => $a){
		$question = str_replace("_", " ",$question);
		$display .= "$question" . ":" . "$a <br />\\r";
	}
}

?>
<html>
<body>
<?php
echo '<form action="" method="post">';
	$currentQ = count($_SESSION['answers']);
	
	//check to see if question is available//
	if(array_key_exists($currentQ,$questions)){
	
		//check that session answers is set and question key is not in array
		if(isset($_SESSION['answers']) && !array_key_exists($currentQ,$_SESSION['answers'])){
			foreach($answers[$currentQ] as $k => $qtext){
				echo "<input type=\\"radio\\" name=\\"{$questions[$currentQ]}\\" value=\\"$k\\" />$qtext<br />\\r";
			}
		}
		echo '<input type="submit" name="submit" value="Submit" />';
	}else{
		$button = (empty($display) ? '<input type="submit" name="showresult" value="Show Result" />' : '<input type="submit" name="clearresults" value="Clear Results" />');
		echo $button;
	}
echo '</form>';

if(isset($display)){ echo $display;}
/*
echo "<pre>";
print_r($questions);	
echo "</pre>";
echo "<pre>";
print_r($answers);	
echo "</pre>";
echo "<pre>";
print_r($_SESSION['answers']);	
echo "</pre>";
*/
?>

Now if the question being asked is not sequential, but rather based on the last answer, then the arrays and whole process will need to be different.

Ha ha, This isn’t Jeopardy, listing a bunch of answers without the question. Here’s a version with the question.

<?php
session_start();

$questions = array(	
	"This is question one",
	"This is question two",
	"This is question three",
	"This is question four",
	"This is question five",
	"This is question six"
);
$answers = array(
	
	0 => array(
		"answer1a",
		"answer1b",
		"answer1c",
		"answer1d",
		"answer1e"),
	1 => array(
		"answer2a",
		"answer2b",
		"answer2c",
		"answer2d",
		"answer2e"),
	2 => array(
		"answer3a",
		"answer3b",
		"answer3c",
		"answer3d",
		"answer3e"),
	3 => array(
		"answer4a",
		"answer4b",
		"answer4c",
		"answer4d",
		"answer4e"),
	4 => array(
		"answer5a",
		"answer5b",
		"answer5c",
		"answer5d",
		"answer5e"),
	5 => array(
		"answer6a",
		"answer6b",
		"answer6c",
		"answer6d",
		"answer6e")
);	
							
///////////////////////////////////////
/////// Clear session answers /////////
///////////////////////////////////////
if(isset($_POST['clearresults'])){
	unset($_SESSION['answers']);
}
							
///////////////////////////////////////
/////Set answer session if not set/////
///////////////////////////////////////
if (!isset($_SESSION['answers'])){$_SESSION['answers'] = array();}

///////////////////////////////////////
///////////// Processing //////////////
///////////////////////////////////////
if($_SERVER['REQUEST_METHOD'] == 'POST'){
	foreach($_POST as $question => $a){
		//Only use question keys//
		if($question != "submit" && $question != "showresult" && $question != "clearresults"){
			$_SESSION['answers'][$question] = $a;
		}
	}
}

///Build answer display or could be used to insert to DB

if(isset($_POST['showresult'])){
	$display = "";
	foreach($_SESSION['answers'] as $question => $a){
		$question = str_replace("_", " ",$question);
		$display .= "$question" . ":" . "$a <br />\\r";
	}
}

?>
<html>
<body>
<?php
echo '<form action="" method="post">';
	$currentQ = count($_SESSION['answers']);
	
	//check to see if question is available//
	if(array_key_exists($currentQ,$questions)){
	
		//check that session answers is set and question key is not in array
		if(isset($_SESSION['answers']) && !array_key_exists($currentQ,$_SESSION['answers'])){
			echo "<b>{$questions[$currentQ]}</b><br />\\r";
			foreach($answers[$currentQ] as $k => $qtext){
				echo "<input type=\\"radio\\" name=\\"{$questions[$currentQ]}\\" value=\\"$k\\" />$qtext<br />\\r";
			}
		}
		echo '<input type="submit" name="submit" value="Submit" />';
	}else{
		$button = (empty($display) ? '<input type="submit" name="showresult" value="Show Result" />' : '<input type="submit" name="clearresults" value="Clear Results" />');
		echo $button;
	}
echo '</form>';

if(isset($display)){ echo $display;}
/*
echo "<pre>";
print_r($questions);	
echo "</pre>";
echo "<pre>";
print_r($answers);	
echo "</pre>";
echo "<pre>";
print_r($_SESSION['answers']);	
echo "</pre>";
*/
?>
</body>
</html>