Hello There,
I am hoping that someone can help me. I am struggling a little bit with PHP arrays. I am relatively new to PHP programming. I am in the process of writing a web application for a college thesis. The system is an online multiple choice system. I have a web form that selects questions and answers from a MySQL database and presents them to the users via a series of multiple choice questions. The user selects a radio button as an answer for each question and submits the form back to a PHP script.
The script should then ?check? the answers against the database and build a page to show the answers that were correctly or incorrectly answered. I can get this to work roughly by parsing all of the form fields using a while loop and for each run through the loop comparing the answer given against the database answer and outputting a ?correct? or ?incorrect? message. On a small scale this is working, it knows the answers that are correct and incorrect etc.
The script is way to busy however and it is not likely to work in the larger scale. I was thinking that it may be better to write the question id, answer given and correct answer into a data structure like an array. This would allow me to collect the data right up to the end where I could do the checking against the database in one swoop and build the results page. The quiz could be relatively large then but all of the data is available in the array and verifying the answers could be achieved by dissecting the array.
- If I create an array and wish to add another element to it can I use an associative array. The only examples I have seen using array_push() are very simple
$stack = array(
"orange",
"banana"
);
array_push($stack, "apple", "raspberry");
Can I for example use the same array_push command to add a key_pair value to an array??
$stack = array(
"orange" => ?Vitamin C?,
"banana" => ?Potassium?
);
array_push($stack[?apple?] => "Vitamin A");
I have tried a number of variances of this and cannot seem to get it to work.
- I am using a while loop to obtain all of the question and answer values submitted on the form. I am varying a count so that I replicate the name of the form field (?question? . $count) = ?question1? and ?question2? etc. I am then passing this derived variable name to get the value from the $_POST superglobal. This gets me the value that was submitted but it is not permanently storing it. Once the while loop goes around again some of the variables are reassigned. I am looking for a way to permanently add the value to a data structure.
I had thought about using an array along the lines of the following
$test_array= array (
$question_id => array(
"sesson_id" => $session_id,
"answer_given" => $answer_id,
"right_answer" => $right_answer_id,
"quiz_q_no" => $quiz_q_count)
);
If however I put this within the while loop the array is created every time and the previous array is destroyed.
- Is it possible to create the $test_array= array (); outside of the while loop and push values on inside of the while loop? Again this comes down to how do I push a key pair value onto an array?
Thanks so much for your help
Regards,
Claire