Hellow all,
I want to ask a question about making quiz script with PHP

if I have an array like this : $pet = array("angora","bird","chipmunk","dinosaurus","eagle","finch"); and I want to make a question like this

Do you have angora ?
oYes oNO
|submit|

when user click "submit" button, then it will setcookie to save the answer, whether the user answer "yes" or "no"..

after that, it will show you the secong question

Do you have bird ?
oYes oNO
|submit|

and it will do the same thing until all question asked..
in the final is, we got 6 quesion (because there are 6 index in array)
and 6 cookie..

thx you so much for your attention
p.s : sorry for my bad english

you would have to use post or get variables,
so do something like

<form action='script.php?q=$nextQuestion'>
(Question)
(Answers)

post your answer and apply a check in php that if the answer is yes then go to new condition area

$answer1=$_REQUEST;
$answer2=$_REQUEST;
$answer3=$_REQUEST;
$answer4=$_REQUEST;

for example

if($answer1=='yes')
{
$question_area=2;
}
if($answer2=='yes')
{
$question_area=3;
}
if($answer1=='yes')
{
$question_area=4;
}

<form name="frm" action="" method="post" >
<?
if($question_area==2)
{
?>
here is question2?
 <input type="checkbox" value="yes" name="chk1" /><br /><br />
<input type="checkbox" value="no" name="chk1"  /><br /><br />
<input type="submit" name="btn" value="Submit"  />
<?
}
elseif($question_area==3)
{
?>
here is question3?
 <input type="checkbox" value="yes" name="chk2" /><br /><br />
<input type="checkbox" value="no" name="chk2"  /><br /><br />
<input type="submit" name="btn" value="Submit"  />
<?
}
elseif($question_area==4)
{
?>
here is question4?
 <input type="checkbox" value="yes" name="chk3" /><br /><br />
<input type="checkbox" value="no" name="chk3"  /><br /><br />
<input type="submit" name="btn" value="Submit"  />
<?
}
else
{
?>
Do you have bird? //question one will show at first page
 <input type="checkbox" value="yes" name="chk4" /><br /><br />
<input type="checkbox" value="no" name="chk4"  /><br /><br />
<input type="submit" name="btn" value="Submit"  />
<?
}
?>
</form>

$answer1=$_REQUEST;
$answer2=$_REQUEST;
$answer3=$_REQUEST;
$answer4=$_REQUEST;

for example

if($answer1=='yes')
{
$question_area=2;
}
if($answer2=='yes')
{
$question_area=3;
}
if($answer1=='yes')
{
$question_area=4;
}

<form name="frm" action="" method="post" >
<?
if($question_area==2)
{
?>
here is question2?
 <input type="checkbox" value="yes" name="chk1" /><br /><br />
<input type="checkbox" value="no" name="chk1"  /><br /><br />
<input type="submit" name="btn" value="Submit"  />
<?
}
elseif($question_area==3)
{
?>
here is question3?
 <input type="checkbox" value="yes" name="chk2" /><br /><br />
<input type="checkbox" value="no" name="chk2"  /><br /><br />
<input type="submit" name="btn" value="Submit"  />
<?
}
elseif($question_area==4)
{
?>
here is question4?
 <input type="checkbox" value="yes" name="chk3" /><br /><br />
<input type="checkbox" value="no" name="chk3"  /><br /><br />
<input type="submit" name="btn" value="Submit"  />
<?
}
else
{
?>
Do you have bird? //question one will show at first page
 <input type="checkbox" value="yes" name="chk4" /><br /><br />
<input type="checkbox" value="no" name="chk4"  /><br /><br />
<input type="submit" name="btn" value="Submit"  />
<?
}
?>
</form>

That is a lot of repeated code. There is an easier way.

I am working on the code now.

Here it is. It is pretty basic, but will do what is required.

<?php

session_start();

$thispage = 'test.php';
$pets = array( 'dog','cat','fish','lizard','alligator','moose','cow' );
$vowels = array( 'a','e','i','o','u' );

function getQuestion( $q ) {
	global $pets,$vowels;
	$curPet = $pets[$q];
	$article = ( in_array( substr( $curPet,0,1 ),$vowels ) ? 'an' : 'a' );
	return "Do you have {$article} {$curPet}?";
}

if ( isset( $_GET['reset'] ) ) {
	unset( $_SESSION['q'] );
	unset( $_SESSION['answer'] );
	header("Location: {$thispage}");
}

if ( !isset( $_SESSION['q'] ) ) {
	$_SESSION['q'] = 0;
}
if ( !isset( $_SESSION['answer'] ) ) {
	$_SESSION['answer'] = array();
}

if ( isset( $_POST['submit'] ) ) {
	$answer = $_POST['answer'];
	if ( $answer == 'Yes' || $answer == 'No' ) {
		$next = ( $_SESSION['q'] + 1 );
		if ( array_key_exists( $next,$pets ) ) {
			$_SESSION['answer'][$_SESSION['q']] = $answer;
		}
		$_SESSION['q'] = $next;
	}
}

if ( $_SESSION['q'] >= count( $pets ) ) {
	$html = '';
	foreach( $_SESSION['answer'] as $q => $value ) {
		$html .= getQuestion( $q ) . " Answer: {$value}<br />";
	}
	$html .= "<br /><br /><a href=\"{$thispage}?reset=Y\">Reset Quiz</a>";
}
else {
	$question = getQuestion( $_SESSION['q'] );
	$html =<<<HTML
<html>
<body>
<h1>{$question}</h1>
<form action="{$thispage}" method="post">
	<input type="radio" name="answer" value="Yes" />&nbsp;Yes<br />
	<input type="radio" name="answer" value="No" />&nbsp;No<br />
	<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
HTML;
}

echo $html;

?>
This article has been dead for over six months. Start a new discussion instead.