...

View Full Version : Resolved Store several words in a session array and then show them



cast_no_shadow
11-05-2012, 02:08 PM
I'm writting a quiz. First the question is shown in question.php then in answer.php, the answer is analised, if the answer is right the word is stored in an array and if wrong in another. Then go back to question.php and so on. When there are no more questions, the result.php will show which words have been correct and which ones were wrong.

Please note that the session array needs to include a new word every time the answer.php is loaded.

My code:

answer.php


<?php
session_start();

$question = $_GET['question'];
$user_answer = $_GET['user_answer'];
$right_answer = mysql_query("SELECT french FROM words where english='$question'");
$fila = mysql_fetch_assoc($right_answer);
$fila_french = $fila['french'];

if ( $user_answer == $fila_french ) {
echo "Right";
$_SESSION['right_words'] = array();
array_push($_SESSION['right_words'], $question);
}

else { echo "Wrong";
$_SESSION['wrong_words'] = array();
array_push($_SESSION['wrong_words'], $question);
}
echo "<a href='question.php' > Next </a>";
?>

result.php :


<?php
session_start();

$right_answers = implode(',',$_SESSION['right_words']);
$wrong_answers = implode(',',$_SESSION['wrong_words']);

echo "<h1> You finished the test </h1>
<p> You have these words right: $right_answers</p>
<p> You have these words wrong: $wrong_answers</p>
<a href='../exercises.php'> Go back to exercises </a>";
?>

The problem is that the session array is only storing the first item because the assignment to array() wipes out anything that was there previously and creates a new empty array.

Thanks for you help!

Fou-Lu
11-05-2012, 02:19 PM
Yep, that's easily fixed:


if (!isset($_SESSION['right_words']))
{
$_SESSION['right_words'] = array();
}
//. . .
if (!isset($_SESSION['wrong_words']))
{
$_SESSION['wrong_words'] = array();
}

Simply check if it exists before attempting to create it. If it does, then don't create it again.
This can be done anytime before you intend to use the applicable array.

cast_no_shadow
11-05-2012, 02:32 PM
Yep, that's easily fixed:


if (!isset($_SESSION['right_words']))
{
$_SESSION['right_words'] = array();
}
//. . .
if (!isset($_SESSION['wrong_words']))
{
$_SESSION['wrong_words'] = array();
}

Simply check if it exists before attempting to create it. If it does, then don't create it again.
This can be done anytime before you intend to use the applicable array.

You mean like this?:


if ( $user_answer == $fila_french ) { echo "Right";

if (!isset($_SESSION['right_words']))
{ $_SESSION['right_words'] = array();}

else { array_push($_SESSION['right_words'], $question);}

}

else { echo "Wrong";

if (!isset($_SESSION['wrong_words']))
{ $_SESSION['wrong_words'] = array();}

else { array_push($_SESSION['wrong_words'], $question);}
}

Fou-Lu
11-05-2012, 04:45 PM
No else statements. Just the if. You want to push onto it either way, you only want to initialize the array if it has not yet been initialized.

cast_no_shadow
11-05-2012, 05:37 PM
No else statements. Just the if. You want to push onto it either way, you only want to initialize the array if it has not yet been initialized.

Thanks for your answer, but it is not working, with or without the else's :(

Fou-Lu
11-05-2012, 05:39 PM
Thanks for your answer, but it is not working, with or without the else's :(

Post the entire code you currently have.

cast_no_shadow
11-05-2012, 05:51 PM
Post the entire code you currently have.

answer.php


<?php

session_start();

// Contect to de DB
include("config.php");

$lesson_n = $_SESSION['lesson_n'];
$question = $_GET['question'];
$user_answer = $_GET['user_answer'];
$right_answer = mysql_query("SELECT french FROM words where english='$question'");
$fila = mysql_fetch_assoc($right_answer);
$fila_french = $fila['french'];

// Get the audio
$audio = mysql_query("SELECT audio_name FROM words where english='$question'");
$audio2 = mysql_fetch_array($audio);

// Know how many words (rows are in the lesson)
$query = mysql_query("SELECT * FROM words where lesson_number='$lesson_n'");
$num_rows = mysql_num_rows($query);

echo "<p> Lesson nš $lesson_n </p>";
echo "<p> Question $question </p>";



if ( $user_answer == $fila_french ) { echo "Right";

if (!isset($_SESSION['right_words']))
{ $_SESSION['right_words'] = array();}


}

else { echo "Wrong";

if (!isset($_SESSION['wrong_words']))
{ $_SESSION['wrong_words'] = array();}

}


echo "<p> Your answer was $user_answer </p>";
echo "<p> The right answer is $fila_french
<a href='../audio/".$audio2['audio_name'].".mp3' class='sm2_button'></a>
</p>";

echo "<a href='question.php' > Next </a>";



?>

and then question.php


<?php

session_start();
$lesson_n = $_SESSION['lesson_n'];

// Contect to de DB
include("config.php");

// Know how many words (rows) are in the lesson
$query = mysql_query("SELECT * FROM words where lesson_number='$lesson_n'");
$num_rows = mysql_num_rows($query);

// Order of the words
if(empty($_SESSION['count'])) $_SESSION['count'] = 0;
$order = $_SESSION['count']+1;
$_SESSION['count'] = $order;
$order_query = $order-1;

// Print the word in french and in english
$result = mysql_query("SELECT * FROM words where lesson_number='$lesson_n' ORDER BY id LIMIT $order_query,1");
$info2 = mysql_fetch_array($result);

if ( $num_rows >= $order ) {

echo " <h1>Question</h1>
<p>Lesson nš $lesson_n </p>
<p> Word $order / $num_rows ";

echo "<p>".$info2['english']."</p>";

echo "<form name='bank_words' action='answer.php' method='GET' id='theFormID'>
<input type='hidden' name='question' value='".$info2['english']."' />
<input type='text' name='user_answer' autocomplete='off' id='theFieldID' autofocus='autofocus' autofocus />
<script type='text/javascript'>theFormID.theFieldID.focus();</script>


<input class='big_b' type='submit' value='check the answer' />

</form> ";

}

// When there are no more words you finish the test and get the results
else {

$right_answers = implode(',',$_SESSION['right_words']);
$wrong_answers = implode(',',$_SESSION['wrong_words']);


echo "<h1> You finished the test </h1>
<p> You have these words right: $right_answers</p>
<p> You have these words wrong: $wrong_answers</p>

<a href='../exercises.php'> Go back to exercises </a>

";}


?>

Fou-Lu
11-05-2012, 06:52 PM
You don't have the array_push in the answers.php. You've initialized the arrays only once, but you don't add anything to them.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum