Jump to content

Help with Arrays and Sessions


Ryanz

Recommended Posts

Hi,

I need some help with a script to do multiple choice questions

 

I started with a script for a simple series of questions which worked that if you got the question wrong then you had to go back to the beginning and start again

 

I then wanted to choose the questions at random from the selection using arrays so that the questions then come up in a different order each time.

This is the script here.

 

I have removed the array for the minute so that the script works

 

Here is the question page:

 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
        <title>Quiz</title>

</head>

<body>

<h1>Q<?php 
$question = $_POST['question'];
echo $question; ?>


</h1><br><b>What is the name of the gland below the adrenal glands?</b><br><br>
<form method="post" action="answer.php">
<input type="radio" name="answer" value="1" />Thyroid<br>
<input type="radio" name="answer" value="2" />pituitary<br>
<input type="radio" name="answer" value="3" />pancreas<br>
<input type="radio" name="answer" value="4" />ovary<br>
<input type="radio" name="answer" value="5" />testis<br>
<input type="hidden" name="correct" value="3" />
<input type="hidden" name="question" value="

<?php echo $question; ?>" />
<input type="Submit" name="submit" value="Submit" />
</form>




</body>
</html>

 

Here is the page which processes the values sent from the question page ...

 

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <title>Quiz</title>
</head>

<body>


<?php 
$answer = $_POST['answer'];
$correct = $_POST['correct'];
$question = $_POST['question'];



$questions = 28; //number of questions

$nextq = rand(1,$questions);

$nextqn = $question + 1;

$newarray = array_splice($arr, array_search($nextq, $arr), 1);

if($answer == $correct) echo "<font color=green>CORRECT</font><br><form action=bq" . $nextq . ".php method=post>
<input type=hidden name=question value=" . $nextqn . ">
<input type=submit name=Next value='Next Question'></form>";
else echo "INCORRECT<br><a href=index.php>Try again ...</a>";



?>

</body>
</html>

 

see it posted at my site

http://www.zimph.com/edu

 

 

The next stage is where I am stuck. I want to not repeat the same question  ie to take it the question out of the array so that it only comes

up once in any sequence of questions. The idea is that it becomes a game to see how many questions you get right before you get the question wrong

a bit like the impossible quiz if you know that. Someone from another forum gave me this script which takes the question out of the array but this is all on one page

whereas I use mutiple pages. I am not clear how to integrate the two scripts to get what I want. Can you give me any direction on what to do next.

 

Thanks for all the help I have already recieved, I have still not found an answer though!

Link to comment
Share on other sites

try

<?php
session_start();
if (isset($_SESSION['q']) and count($_SESSION['q']) > 0) $q = $_SESSION['q']; else $q = range(1, 28);
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <title>Quiz</title>
</head>

<body>


<?php 
$answer = $_POST['answer'];
$correct = $_POST['correct'];
$question = $_POST['question'];



$questions = 28; //number of questions
$nq = array_rand($q,1); 
$nextq = $q[$nq]; //rand(1,$questions);
unset($q[$nq]);
$_SESSION['q'] = $q;

$nextqn = $question + 1;

//$newarray = array_splice($arr, array_search($nextq, $arr), 1);

if($answer == $correct) echo "<font color=green>CORRECT</font><br><form action=bq" . $nextq . ".php method=post>
<input type=hidden name=question value=" . $nextqn . ">
<input type=submit name=Next value='Next Question'></form>";
else echo "INCORRECT<br><a href=index.php>Try again ...</a>";



?>

</body>
</html>

Link to comment
Share on other sites

try

<?php
session_start();
if (isset($_SESSION['q']) and count($_SESSION['q']) > 0) $q = $_SESSION['q']; else $q = range(1, 28);
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <title>Quiz</title>
</head>

<body>


<?php 
$answer = $_POST['answer'];
$correct = $_POST['correct'];
$question = $_POST['question'];



$questions = 28; //number of questions
$nq = array_rand($q,1); 
$nextq = $q[$nq]; //rand(1,$questions);
unset($q[$nq]);
$_SESSION['q'] = $q;

$nextqn = $question + 1;

//$newarray = array_splice($arr, array_search($nextq, $arr), 1);

if($answer == $correct) echo "<font color=green>CORRECT</font><br><form action=bq" . $nextq . ".php method=post>
<input type=hidden name=question value=" . $nextqn . ">
<input type=submit name=Next value='Next Question'></form>";
else echo "INCORRECT<br><a href=index.php>Try again ...</a>";



?>

</body>
</html>

 

Thanks for the reply, it seems to be just what I need, although I can't get it to work! It's currently on the site: zimph.com/edu ... I used the exact script you gave me, and I can't see what is wrong with it, although it still doesn't seem to work ...

Link to comment
Share on other sites

what is wrong

 

 

Basiaclly the questions appeared randomly as usual ... but they still repeated themselves without running through all the questions ...

 

The php all seemed to run ... just not in the right way ... I can't see why it shouldn't work ...?

Link to comment
Share on other sites

are you have cookies enabled in your browser

 

Hey

 

I have it working now! ... but there is one more thing I want to add, and for some reason I'm unable to :S

 

For the first question I want to create the array using your script, but then when all the questions have finished I wanted the script to say complete.

 

I changed the else statment when checking for the session variable, so that if there is no array present, "Complete" would be echoed

 

<?php
session_start();
if (isset($_SESSION['q']) and count($_SESSION['q']) > 0) $q = $_SESSION['q'];
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <title>Quiz</title>
</head>

<body>



<?php 
else echo "Complete";
$answer = $_POST['answer'];
$correct = $_POST['correct'];
$question = $_POST['question'];



$questions = 5; //number of questions
$nq = array_rand($q,1); 
$nextq = $q[$nq]; //rand(1,$questions);
unset($q[$nq]);
$_SESSION['q'] = $q;

$nextqn = $question + 1;

//$newarray = array_splice($arr, array_search($nextq, $arr), 1);

if($answer == $correct) echo "<font color=green>CORRECT</font><br><form action=bq" . $nextq . ".php method=post>
<input type=hidden name=question value=" . $nextqn . ">
<input type=submit name=Next value='Next Question'></form>";
else echo "INCORRECT<br><a href=index.php>Try again ...</a>";



?>

</body>
</html>

 

Could you tell me where I'm going wrong?

 

I create the $q variable in an answer1.php which comes after the first question. The answer1.php script is identical to yours ... and the answer.php script that comes after each question other than the first has the same code as the above ...

 

It would be awesome if you could help!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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