How does the session array work?

Source: Internet
Author: User
The code is as follows
...... Connection Database section

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;//There is a problem here?>

Echo
" ".
"A.". $arrayA [$i]. "".
"'" ' "" Omit b,c,d option
?>

I'm doing an online answer system that receives each user's options through the $answer[] array. I want to use a $_session[] array to store the user's options in bulk (that is, the user's answer results are stored once)
Each time the page reads a selection question instead of a group, clicking Submit will refresh the next question on the "same page". That is, each click to submit the equivalent of refreshing a page, will not be $_session[] mechanism is only one-time submitted data can only be recorded, the second submitted data will be the first time to overwrite the data.
I've tried two ways now, but I haven't succeeded in reading.
Method 1:
$answer []=$_post[' Style1 '];

for ($i =0; $i < $cnt; $i + +) {

$_session[$i +1]= $answer [$i];

}
Although it is a cyclic assignment, but because the post has only one value to $answer[] at a time, so only $array[0] has the user-submitted answer, assigned to $_session[] and only then $_session[1] have the answer, other key values have no answer.
Print out the results
Array ([1] = A [2] = [3] = [4] = [5] = =)

Method 2:
$answer =$_post[' Style1 ']; note that the $answer here are not arrays but variables.
$i = $i +1;
$_session[$i]=array ("id" = $i, "Answer" and "$answer");
Print_r ($_session);
The goal is to create a two-dimensional array with $_session, where the ID is used to record the problem number and answer is used to record options.
The result of each question printed is
Array ([0] = = Array ([id] = 0 [Answer] =)) No click submit button
Array ([1] = = Array ([id] = 1 [answer] + a)) Click the Submit button once, where the title number 1 and user option A ... are recorded.

But when I answer all the choice questions,
Print_r ($_session[5]);
Print ($_session[4]), when only $_session[5] has a value, this is the last question, that is, $_session[$i] only the last question.
$_session[1]?? $_SESSION[4] Where did the values go and why wasn't it recorded?



Please help me to think of there is no good way to make some suggestions for changes


























Reply to discussion (solution)

Each time you receive a submission,
$_session[] = $_post;

The submitted form should have the title number, so that it is easy to identify

I will now change the value of $_session to
$_session=array ("id" =>$_post[' i '], "answer" =>$_post[' Style1 ' ]);
recreate an array
$array =array ("id" = "", "answer" and "=");
Merge $_session and $array recursively into $result
$result =array_merge_recursive ($_session, $array);
2 Arrays do not have values when reading the first question, so the result of printing
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
Arra Y ([id] = 1 [answer] + A)
Array ([id] = [answer] =)
Array ([id] = = array ([0] = 1 [ 1] = [answer] = = Array ([0] + A [1] = =))

If you want to add the answer to the second question and the title number, I think you have to let the
$result 1=array_merge _recursive ($_session, $result);
If you want to add the answer and question number of the third question to need
$result 2=array_merge_recursive ($_session, $result 1);
Now the question is
$result 1 where is not appropriate, and whether it is reasonable to do so, because if I have n a problem, then I need to N-1 a $result


You haven't done any actual research projects (offline and online)?

There is no need to merge the questionnaire and the answer sheet, the only function of merging is to omit the last output report from reading the database (the questionnaire part).
Even if the middle of the statistical process is generally ignored questionnaires, if the answer is also put into storage, it is even more unnecessary

In addition, the questionnaire problem volume is not too large, if very large, to answer is not a person, but a machine
In general, the next time you read it into session, the output to the client depends on the question or output of the survey.
The special situation is the subsequent problem (question bank), and the previous answer about (jump and branch large, logical relationship complex), so that you need to read each library

Based on the above two points, the general situation----Read the topic one at a time, the answer is recorded in session
Special case, the ID is credited to the session each time you read the question
The basic principle is that the topic is read into the session, and then sent to the client, not the client after the answer to the session, carefully handle their correspondence is OK

It's simple.
1, the session stored in the answer array to assign a temporary array $answer;
2, the last page submitted by the answer out to merge with $answer to $new_answer;
3. Overwrite the $new_answer into the session array where the answer is stored;
4. Show 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.