PDA

View Full Version : multidimensional arrays?


grantypap
05-08-2007, 05:03 PM
hey guys,

i have a form for a php/mysql based online test.

at present the code calls the questions and their unique ID's and puts the questions in a table and adds the ID as a hidden field.
the problem that i am having is that im not sure how i am going to put the answers back in the DB.

i cant just use the POST method because there may be hundreds of entry's so i was thinking of using 2 arrays, 1 for ID's and 1 for questions.
ie. the first entry in ID_array will match the first entry in QUESTION_array.

this would work fine but for some questions there can be more than one possible answer.
do you think that multidimensional arrays may be the way to go?
if so how would you suggest i implement this?

Cheers,
Grant

rafiki
05-08-2007, 05:08 PM
store the answer into the database as soon as they go on to the next question via AJAX or over a series of pages so questions 1-5 on page 1
on page 2 store the answers and questions 6-10 etc..?

arne2
05-08-2007, 05:12 PM
You can save their current answers to a variable and then put it in the database as you want.

So a quick example of what i mean
everytime they answer a question, their answer gets added to the $answers variable.

so something like this:
if(!empty($answers)){
$answers="$answers,$_POST['answer']";
}else{
$answer="$_POST['answer']";
}

ASK YOUR QUESTOIN HERE AND POST IT AS ANSWER

After all answers are done you can save them in a database
you can just save $answers to the database but if you want every answer in a seperate field (like fields answer1 answer2,...) you can use the explode function i think, something like this:
$ar_field=explode(",",$answers);



I made this really quick so it's probably with a lot of faults but it will give you an idea i think...

rafiki
05-08-2007, 05:14 PM
like what?
you mean

$answer = $_POST['answer1'];
$answer .= $_POST['answer2'];

and carry that on for every answer
or have yout thought of sessions?

grantypap
05-08-2007, 05:33 PM
both of those options sound along the lines of my thinking.

i have considered just having 1 question at a time (it would be a lot easier!) but ideally i would like all of them to come up at once.

what i want to do is somehow tie the question ID in with the answer.
maybe concatenating the answers with the ID and breaking it up on the script side may work?

i starting to go crazy over this now!!! lol

rafiki
05-08-2007, 05:42 PM
i wouldnt recommend putting 100's of questions on one page thats a lot of scolling and messing around i would rather answer 10 per page

grantypap
05-08-2007, 05:59 PM
yeah, your right but there may only be say 30.