Let's say I have a pretty small static form with a few variables.
<form action='.$PHP_SELF.' method=POST>
Name:<input type=text name=Name>
Sex:
<input type="radio" name="sex" value="male">
<input type="radio" name="sex" value="female">
<input type="radio" name="sex" value="other">
<input type="submit" name="submit" value="Submit it!">
The script to process it would be fairly simply.
We'd end up with two variables set to some value:
$Name = 'Joe Schmo';
$sex = 'Other';
And manipulating these variables works just like manipulating any other variables in php.
echo $Name;
echo '<br/>';
echo $sex;
The draw back to this is, when we add another question to our form, say:
Favorite Color:
<input type="radio" name="favcolor" value="Red">
<input type="radio" name="favcolor" value="Blue">
<input type="radio" name="favcolor" value="Green">
<input type="radio" name="favcolor" value="Other">
we then have to alter our script and add some function to the new, unexpected variable $favcolor
echo $favcolor;
Now, if we were to devise a system where a user could add questions, and then answers to those questions to be saved in a database to later be read back and automatically put into a form, we could not use the easy name="what my variable will be" format. If we have, over 100 quizes, 1000 questions we can't realistically write a script that accounts for all of them:
if($somevar)
{
do something
}
if($some_other_var)
{do something else}
...
if($our_100thvar)
{
do something
}
and even if we did want to write this script, maintain it would be hellish. Every time a user added a question we would have to edit the script to make sure there was a rule for it.
So,
we have to find some way of having:
echo $question_name;
echo '<input type=radio name='.$question_variable_name.' value='.$answer_value.'>';
echo '<input type=radio name='.$question_variable_name.' value='.$answer_value.'>';
echo '<input type=radio name='.$question_variable_name.' value='.$answer_value.'>';
Still, if we allow the user to define the
$question_variable_name and $answer_value we'd have to go back and write code for dealing with all these new variables.
My idea was to use question and answer IDs to print these
so we might have a question with data called question_id = 2
question_title = Sex
and some answers
answer_id = 200
question_id = 2
answer_title = Male
answer_id = 201
question_id = 2
answer_title = Female
answer_id = 202
question_id = 2
question_title = Other
Reading from the database we would get output of HTML in the format:
Sex
Male <input type=radio name=2 value=200>
Female <input type=radio name=2 value=201>
Other <input type=radio name=2 value=202>
Then you could write to the database and say
"User # 678 answered Quiz #1 question #2 with answer #202"
two obvious problems:
1) variables can't be named $1, $2, $5, etc
2) You'd have no way of writing a script that can handle new variables unless you had a loop that used some arbitrary high number to do
for ($i = 0; $i < 1000000000; $i++)
{
'query mysql to insert the value of the variables with values into the db
}
That is, the script would go
$i = 0
insert the value of variable $0 into the db
with question_id = 0 and answer_id = the value of $0
$i++
$i = 1
insert the value of variable $1 into the db
with question_id = 1 and answer_id = the value of $1
$i ++
. . .
$i = 780432
insert the value of variable $780432 into the db
with question_id = 780432 and answer_id = the value of $780432
obviously, this is not an option.
to over come the name='.$question_id.' problem is it possible to have
name=var['.$question_id.']
and store the answers in an array?
then at least I could find out how many questions were in a quiz and do a loop for that
so we could end up wth
$var[2] = 202
for the above Sex = Other quesitons
But, and here is my real problem, how do i shuttle around the value of $question_id?
$var['question_id] = answer_id
I want to be able to INSERT
INSERT into quiz (user_id, quiz_id, question_id, answer_id) values ($user_id, quiz_id, question_id, answer_id)
But I am stuck on how to shuttle that question_id around.
my two ideas were:
1)
using var[2] format and trying to somehow rip that "var[" and "]" off of the variable to get the name I need
so
some_funciton("$var[2]")
would return "2"
2) OR
making the name="" in the input generically named with a loop based on $questions_per_page
(i.e. var1, var2, var3, var4, var5)
then having the values of these variables contain both question_id and answer_id
so $var5 = '2,202'
then using
explode
to separate them into to variables I can manipulate.
Long question short, how can I move about that $question_id variable so that when it comes time to INSERT INTO I have the value?