Jump to content

~~ Updating Arrays stroed in sessions and retrieving them? ~~


Ryanz

Recommended Posts

Hey, It's me again ... If you saw my other thread.

 

I was going to send an array through a hidden input in a form as I wanted to have a load of questions randomly come after each other, but I didn't want one question to appear 2 times.

 

People told me I should use sessions ... so this is what I did ... I still can't get it to work. I'm pretty inexperienced in PHP and I litterally learned about sessions then wrote this.

 

There are errors I know, I just can;t fix them :S

 

Here is the first answer page: answer1.php

 


<!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'];



$arr = array("1","2","3","4","5","6","7","8");

session_start(); 

$_SESSION['array'] = $arr;

$random = array_rand($arr);


$nextq = $arr[$random];

$nextqn = $question + 1;

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

if($answer == $correct) echo "<font color=green>CORRECT</font><br><form action=q" . $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>

 

That comes after the first question basiaclly. It creates the array.

 

Here is answer.php which comes after all 10 or so of the next questions ...

 


<!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'];

if(isset($_SESSION['array'])) $_SESSION['array'] = $arr;


$random = array_rand($arr);


$nextq = $arr[$random];

$nextqn = $question + 1;

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

if($answer == $correct) echo "<font color=green>CORRECT</font><br><form action=q" . $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>

 

The first thing I need help with is updating the session variable with "$newarray" and the second thing I need help with is debugging it.

 

I would be very greatful if you could help me with this!

 

Thanks

Ryan

 

Link to comment
Share on other sites

And what was wrong with the solution I provided you in this thread: http://www.phpfreaks.com/forums/index.php/topic,181018.0.html

 

It did exactly what you wanted except it used cookies instead of session. Just change the cookies to sessions and you're golden. Did you even try the code I provided? If so, what does it not do that you need it to?

 

Below is the same code again with session data instead of cookies as well as some add'l changes for clarity. note that most of that code is just there for illustrative purposes to show what is happening. The code allows you to set a finite number of questions for the user and all of those questions will be presented in a random order. The user MUST proceed from one question to the next and cannot manually change the order.

 

<?php

session_start();

$page_count = 10;
$page = $_GET['page'];


if ($page!='Next') {

    //Create an array with the values from 1 to $page_count
    $pages = range(1, $page_count);
    //Randomize the array
    shuffle($pages);
    //Save the array to a session variable
    $_SESSION['pages'] = serialize($pages);

    echo "You are on the start page. There are $page_count random questions to answer.<br><br>";
    echo " Click <a href=\"$_SERVER[php_SELF]?page=Next\">here</a> to start.";

} else {

    //retrieve the pages array from the session
    $pages = unserialize($_SESSION['pages']);
    //Get the first from the random list
    $this_page = array_shift ($pages);
    //Determine if there are additional pages left
    $last_page = (count($pages)>0)?false:true;
    //Save the new pages array to the cookie
    $_SESSION['pages'] = serialize($pages);

    echo "<h2>You are on question $this_page.</h2>";
    echo "Pages <span style=\"background-color:#696969;\">used</span>/remaining:<br><br>";

    //This section is for debugging illustrative purposes only
    for ($i=1; $i<=$page_count ; $i++) {
      if (in_array($i, $pages)) {
        $bgcolor = "#ffffff;";
      } else {
        $bgcolor = "#696969;";
      }
      echo "<span style=\"background-color:$bgcolor;padding:3px;padding-left:6px;padding-right:6px;border:1px solid #000000;\">$i</span> ";
    }

    //Display messages based upon whether this is the last page or not.
    if ($last_page) {
      echo "<br><br>This is the last question.";
    } else {
      echo "<br><br>Go to <a href=\"$_SERVER[php_SELF]?page=Next\">next</a> question.";
    }

    echo "<br><br>Go <a href=\"$_SERVER[php_SELF]\">home</a> and start over.";
}

?>

Link to comment
Share on other sites

And what was wrong with the solution I provided you in this thread: http://www.phpfreaks.com/forums/index.php/topic,181018.0.html

 

It did exactly what you wanted except it used cookies instead of session. Just change the cookies to sessions and you're golden. Did you even try the code I provided? If so, what does it not do that you need it to?

 

Below is the same code again with session data instead of cookies as well as some add'l changes for clarity. note that most of that code is just there for illustrative purposes to show what is happening. The code allows you to set a finite number of questions for the user and all of those questions will be presented in a random order. The user MUST proceed from one question to the next and cannot manually change the order.

 

<?php

session_start();

$page_count = 10;
$page = $_GET['page'];


if ($page!='Next') {

    //Create an array with the values from 1 to $page_count
    $pages = range(1, $page_count);
    //Randomize the array
    shuffle($pages);
    //Save the array to a session variable
    $_SESSION['pages'] = serialize($pages);

    echo "You are on the start page. There are $page_count random questions to answer.<br><br>";
    echo " Click <a href=\"$_SERVER[php_SELF]?page=Next\">here</a> to start.";

} else {

    //retrieve the pages array from the session
    $pages = unserialize($_SESSION['pages']);
    //Get the first from the random list
    $this_page = array_shift ($pages);
    //Determine if there are additional pages left
    $last_page = (count($pages)>0)?false:true;
    //Save the new pages array to the cookie
    $_SESSION['pages'] = serialize($pages);

    echo "<h2>You are on question $this_page.</h2>";
    echo "Pages <span style=\"background-color:#696969;\">used</span>/remaining:<br><br>";

    //This section is for debugging illustrative purposes only
    for ($i=1; $i<=$page_count ; $i++) {
      if (in_array($i, $pages)) {
        $bgcolor = "#ffffff;";
      } else {
        $bgcolor = "#696969;";
      }
      echo "<span style=\"background-color:$bgcolor;padding:3px;padding-left:6px;padding-right:6px;border:1px solid #000000;\">$i</span> ";
    }

    //Display messages based upon whether this is the last page or not.
    if ($last_page) {
      echo "<br><br>This is the last question.";
    } else {
      echo "<br><br>Go to <a href=\"$_SERVER[php_SELF]?page=Next\">next</a> question.";
    }

    echo "<br><br>Go <a href=\"$_SERVER[php_SELF]\">home</a> and start over.";
}

?>

 

The problem is I need an individual page telling the user if they got the question right or wrong, if they get it wrong they should click try again and be redirected to the home page. I'll try and edit your script to see if I can get it to work like this!

 

Thanks alot

Ryan

Link to comment
Share on other sites

And what was wrong with the solution I provided you in this thread: http://www.phpfreaks.com/forums/index.php/topic,181018.0.html

 

It did exactly what you wanted except it used cookies instead of session. Just change the cookies to sessions and you're golden. Did you even try the code I provided? If so, what does it not do that you need it to?

 

Below is the same code again with session data instead of cookies as well as some add'l changes for clarity. note that most of that code is just there for illustrative purposes to show what is happening. The code allows you to set a finite number of questions for the user and all of those questions will be presented in a random order. The user MUST proceed from one question to the next and cannot manually change the order.

 

<?php

session_start();

$page_count = 10;
$page = $_GET['page'];


if ($page!='Next') {

    //Create an array with the values from 1 to $page_count
    $pages = range(1, $page_count);
    //Randomize the array
    shuffle($pages);
    //Save the array to a session variable
    $_SESSION['pages'] = serialize($pages);

    echo "You are on the start page. There are $page_count random questions to answer.<br><br>";
    echo " Click <a href=\"$_SERVER[php_SELF]?page=Next\">here</a> to start.";

} else {

    //retrieve the pages array from the session
    $pages = unserialize($_SESSION['pages']);
    //Get the first from the random list
    $this_page = array_shift ($pages);
    //Determine if there are additional pages left
    $last_page = (count($pages)>0)?false:true;
    //Save the new pages array to the cookie
    $_SESSION['pages'] = serialize($pages);

    echo "<h2>You are on question $this_page.</h2>";
    echo "Pages <span style=\"background-color:#696969;\">used</span>/remaining:<br><br>";

    //This section is for debugging illustrative purposes only
    for ($i=1; $i<=$page_count ; $i++) {
      if (in_array($i, $pages)) {
        $bgcolor = "#ffffff;";
      } else {
        $bgcolor = "#696969;";
      }
      echo "<span style=\"background-color:$bgcolor;padding:3px;padding-left:6px;padding-right:6px;border:1px solid #000000;\">$i</span> ";
    }

    //Display messages based upon whether this is the last page or not.
    if ($last_page) {
      echo "<br><br>This is the last question.";
    } else {
      echo "<br><br>Go to <a href=\"$_SERVER[php_SELF]?page=Next\">next</a> question.";
    }

    echo "<br><br>Go <a href=\"$_SERVER[php_SELF]\">home</a> and start over.";
}

?>

 

I still don't understand how you take the last page number out of the array ...?

Link to comment
Share on other sites

All I need is a way to display the random number, so that I can redirect the person to the question.

 

For example

 

<?php
$i = "random number which only shows once picked from array";

echo "<a href=http://www.zimph.com/edu/q". $i .".php>Next question</a>"

?>

Link to comment
Share on other sites

By putting the next page value ont he query string, you would be making it more difficult to control the order of the questions since the user can edit that value. Take a look at this:

 

<?php

session_start();

$page_count = 10;
$page = $_GET['page'];


if (!isset($_POST['answer'])) {

    //Create an array with the values from 1 to $page_count
    $pages = range(1, $page_count);
    //Randomize the array
    shuffle($pages);
    //Save the array to a session variable
    $_SESSION['pages'] = serialize($pages);

    echo "You are on the start page. There are $page_count random questions to answer.<br><br>";

    echo "<form method=\"POST\" action=\"{$_SERVER['PHP_SELF']}\">\n";
    echo "<button type=\"submit\" name=\"answer\" value=\"start\">Start</button> the test\n";
    echo "<form>\n";

} else {

    //retrieve the pages array from the session
    $pages = unserialize($_SESSION['pages']);

    if ($_POST['answer']=='False') {

        echo "You got the last question <b>wrong</b>. Try again.";
        $this_page = $_POST['current_page'];

    } else {

        $last_page = (count($pages)<1)?true:false;

        //Get the first from the random list
        $this_page = array_shift ($pages);
        //Save the new pages array to the cookie
        $_SESSION['pages'] = serialize($pages);

        if ($_POST['answer']=='Start') {
            echo "This is your first question. Let's start the test.";
        } else {
            echo "You got the last question <b>correct</b>.";
        }
    }

    if ($last_page) {

        echo "<br><br>You have completed the test.";

    } else {

            echo "<h2>You are on question $this_page.</h2>";

            echo "<form method=\"POST\" action=\"{$_SERVER['PHP_SELF']}\">Question goes here:<br>\n";
            echo "<input type=\"hidden\" name=\"current_page\" value=\"{$this_page}\">\n";
            echo "<button type=\"submit\" name=\"answer\" value=\"True\">True</button> \n";
            echo "<button type=\"submit\" name=\"answer\" value=\"False\">False</button> \n";
            echo "<form>\n";

            echo "<br><br><br><br><div style=\"margin-left:30px;background-color:yellow;width:400px;padding:20px;\">";
            echo "<b>This section for illustrative purposes only:</b><br><br>";
            echo "Pages <span style=\"background-color:#696969;\">used</span>/remaining:<br><br>";

            //This section is for debugging illustrative purposes only
            for ($i=1; $i<=$page_count ; $i++) {
              if (in_array($i, $pages)) {
                $bgcolor = "#ffffff;";
              } else {
                $bgcolor = "#696969;";
              }
              echo "<span style=\"background-color:$bgcolor;padding:3px;padding-left:6px;padding-right:6px;border:1px solid #000000;\">$i</span> ";
            }
            echo "<br></div>";

    }

    echo "<br><br>Go <a href=\"$_SERVER[php_SELF]\">home</a> and start over.";

}

?>

Link to comment
Share on other sites

By putting the next page value ont he query string, you would be making it more difficult to control the order of the questions since the user can edit that value. Take a look at this:

 

<?php

session_start();

$page_count = 10;
$page = $_GET['page'];


if (!isset($_POST['answer'])) {

    //Create an array with the values from 1 to $page_count
    $pages = range(1, $page_count);
    //Randomize the array
    shuffle($pages);
    //Save the array to a session variable
    $_SESSION['pages'] = serialize($pages);

    echo "You are on the start page. There are $page_count random questions to answer.<br><br>";

    echo "<form method=\"POST\" action=\"{$_SERVER['PHP_SELF']}\">\n";
    echo "<button type=\"submit\" name=\"answer\" value=\"start\">Start</button> the test\n";
    echo "<form>\n";

} else {

    //retrieve the pages array from the session
    $pages = unserialize($_SESSION['pages']);

    if ($_POST['answer']=='False') {

        echo "You got the last question <b>wrong</b>. Try again.";
        $this_page = $_POST['current_page'];

    } else {

        $last_page = (count($pages)<1)?true:false;

        //Get the first from the random list
        $this_page = array_shift ($pages);
        //Save the new pages array to the cookie
        $_SESSION['pages'] = serialize($pages);

        if ($_POST['answer']=='Start') {
            echo "This is your first question. Let's start the test.";
        } else {
            echo "You got the last question <b>correct</b>.";
        }
    }

    if ($last_page) {

        echo "<br><br>You have completed the test.";

    } else {

            echo "<h2>You are on question $this_page.</h2>";

            echo "<form method=\"POST\" action=\"{$_SERVER['PHP_SELF']}\">Question goes here:<br>\n";
            echo "<input type=\"hidden\" name=\"current_page\" value=\"{$this_page}\">\n";
            echo "<button type=\"submit\" name=\"answer\" value=\"True\">True</button> \n";
            echo "<button type=\"submit\" name=\"answer\" value=\"False\">False</button> \n";
            echo "<form>\n";

            echo "<br><br><br><br><div style=\"margin-left:30px;background-color:yellow;width:400px;padding:20px;\">";
            echo "<b>This section for illustrative purposes only:</b><br><br>";
            echo "Pages <span style=\"background-color:#696969;\">used</span>/remaining:<br><br>";

            //This section is for debugging illustrative purposes only
            for ($i=1; $i<=$page_count ; $i++) {
              if (in_array($i, $pages)) {
                $bgcolor = "#ffffff;";
              } else {
                $bgcolor = "#696969;";
              }
              echo "<span style=\"background-color:$bgcolor;padding:3px;padding-left:6px;padding-right:6px;border:1px solid #000000;\">$i</span> ";
            }
            echo "<br></div>";

    }

    echo "<br><br>Go <a href=\"$_SERVER[php_SELF]\">home</a> and start over.";

}

?>

 

Thanks a huge amount for that!

 

I'm almost there ...

 

There's still one thing though ... How can I put more than one question in?

 

It's multiple choice questiong that I want to integegate ... If I edit the forum and change the correct answer value I think it will work. But when the user gets A question wrong, I don't want them to have the chance to go back and correct their mistake

Link to comment
Share on other sites

I only created a simple true/false response to test the functionality. The true/false should NOT be the value of the answers. Instead you should be creating the questions and possible answers dynamically. Then you should be checking the response form the user and determining if the response was true or false and taking appropriate action.

 

 

Link to comment
Share on other sites

I only created a simple true/false response to test the functionality. The true/false should NOT be the value of the answers. Instead you should be creating the questions and possible answers dynamically. Then you should be checking the response form the user and determining if the response was true or false and taking appropriate action.

 

 

 

Can you tell me how I can interigate more than one question?

 

I'm using this script to help me to revise for my GCSEs you see ... I'm 15 and I thought it would be a good idea as I have exams coming up ...

 

Your script is almost what I need ... I just need some help understanding how to create more questions and also maybe mixing up the answers each time ...

 

The last script you gave me was great ... but it doesn't do exactyl what I want it to do ... could you have one more go at it.

 

Visit www.zimph.com/edu to check out the script I wrote already which uses multiple pages. The script I wrote is what I want, but I want to questiosn to only appear once and the answer order to be mixed up ...

 

It would be great if you could once again help :P

 

Cheers

Ryan

Link to comment
Share on other sites

Sorry, but I'm not going to invest a ton of time doing all this for you. this forum is for people who want help with their code. You can incorporate as many questions as you want, just create the code to pull and display the question and answers. I only put one question in repeated multiple times to concentrate on the functionality you asked for. Good luck.

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.