How to use the SESSION array? -Php Tutorial

Source: Internet
Author: User
How to use the SESSION array? The code is as follows:
...... Database Connection

While ($ row = mysql_fetch_array ($ myquery )){
$ Arrayid [] = $ row [id];
$ Array [] = $ row [question];
...... Assign the corresponding column to the corresponding array
}
$ Cnt = count ($ arrayid );

$ Answer [] =$ _ POST ['style1'];
$ _ SESSION [] = $ answer; // Is there a problem here?>

Echo
" ".
"A.". $ arrayA [$ I]. "".
"'Omit option B, C, D
?>

I am working on an online Q & A system that receives the option of each user through the $ answer [] array. I want to use a $ _ SESSION [] array to store user options in batches (that is, to store the user's answer results at one time)
Each time the page reads a multiple-choice question, instead of a group, click submit to refresh the next question on the "same page. That is, if you click submit once, it is equivalent to refreshing the page. Will it be $ _ SESSION []? only the data submitted at a time can be recorded, the data submitted for the second time will overwrite the data for the first time.
I tried two methods in total, but the Reading failed.
Method 1:
$ Answer [] =$ _ POST ['style1'];

For ($ I = 0; $ I <$ cnt; $ I ++ ){

$ _ SESSION [$ I + 1] = $ answer [$ I];

}
Although it is a cyclic value assignment, since only one value is transferred to $ answer [] at a time in the POST, only $ array [0] has the answer submitted by the user, after being assigned to $ _ SESSION [], only $ _ SESSION [1] has an answer, and no other key values have an answer.
Output result
Array ([1] => A [2] => [3] => [4] => [5] =>)

Method 2:
$ Answer = $ _ POST ['style1']; note that $ answer is not an array but a variable.
$ I = $ I + 1;
$ _ SESSION [$ I] = array ("id" => $ I, "answer" => $ answer );
Print_r ($ _ SESSION );
The purpose is to create a two-dimensional array with $ _ SESSION, where id is used to record question numbers and answer is used to record options.
The output result of each question is
Array ([0] => Array ([id] => 0 [answer] =>) without clicking the submit button
Array ([1] => Array ([id] => 1 [answer] => A) click the submit button. the question 1 and user option A... are recorded here ......

However, when I answer all the multiple choice questions
Print_r ($ _ SESSION [5]);
Print ($ _ SESSION [4]); only $ _ SESSION [5] has a value. this is the last question, that is to say, $ _ SESSION [$ I] only has the last question.
$ _ SESSION [1]? Why is the value in $ _ SESSION [4] not recorded?



Could you please help me think about any good methods and make some suggestions for modification?


























Reply to discussion (solution)

Each time you receive a submission
$ _ SESSION [] = $ _ POST;

It is recommended that the submitted form have question numbers for easy identification.

Now I want to change the $ _ SESSION value
$ _ SESSION = array ("id" = >$ _ POST ['I'], "answer" = >$ _ POST ['style1']);
Recreate an array
$ Array = array ("id" => "", "answer" => "");
Combine $ _ SESSION and $ array recursively to $ result
$ Result = array_merge_recursive ($ _ SESSION, $ array );
When I read the first question, both arrays have no values, so the output result is printed.
Array ([id] => [answer] =>) // SESSION
Array ([id] => [answer] =>) // ARRAY
Array ([id] => Array ([0] =>) [answer] => Array ([0] =>) // RESULT
Submit the first question and read the second question
Array ([id] => 1 [answer] =>)
Array ([id] => [answer] =>)
Array ([id] => Array ([0] => 1 [1] =>) [answer] => Array ([0] => A [1] => ))

If you want to add the answer and question number of the second question
$ Result1 = array_merge_recursive ($ _ SESSION, $ result );
If you want to add the answer and question number of the third question
$ Result2 = array_merge_recursive ($ _ SESSION, $ result1 );
The problem is
$ Result1 is not suitable where to put it, and is it reasonable to do so, because if I have N questions, I need a N-1 $ result


Have you ever performed any actual investigation project (offline or online?

There is no need to merge the questionnaire and the answer sheet. the only function of the merge is to output the report to save the effort of reading the database (the questionnaire part ).
Even if the questionnaire is ignored during the statistical process, it is even more unnecessary if the answers are stored in the database.

In addition, the number of questions in the questionnaire is not too large. if there is a large number of questions, the answer is not a person, but a machine.
Generally, one session can be read and put into the session, and the output to the client depends on the investigation needs to be output by question or once.
In special cases, questions are not fixed in the future (Question Bank) and are related to the previous answer (jump and the branch is huge, and the logical relationship is complex). In this way, you need to read the database for each question.

Based on the above two points, generally --> A read question is recorded in the session once, and the answer is recorded in the session one by one
In special cases, the id is recorded in the session for each read question.
The basic principle is that a question is recorded in the session before it is sent to the client, rather than recorded in the session only after the client answers the question. just be careful with the corresponding relationship.

It's easy.
1. obtain the array storing the answer in the session and assign the value to the temporary array $ answer;
2. Merge the answers submitted on the previous page with $ answer into $ new_answer;
3. overwrite $ new_answer to the session array storing the answer;
4. show the next question

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.