answers$n. php tries to concate the constant answers with the variable
$n, but you forgot the concatenation operator ".".
i assume that answers should be a string and
is not a constant, therefore $answer["answers".$n] is right.
if answers is a constant use $answer[answers.$n];
but a better way of what you try to accomplish would be a array
using two dimensions:
$answer["answers"][$n]
$answer["question"][$n] ...etc
or if you only have "answers" as key use:
$answer[$n]

if you want to send form data as array, using more than one dimension,
do the following:

<input type="text" name="answers[answer][1]" value="">
<input type="text" name="answers[answer][2]" value="">
<input type="text" name="answers[answer][3]" value="">

or

<input type="text" name="answers[answer][]" value="">
<input type="text" name="answers[answer][]" value="">
<input type="text" name="answers[answer][]" value="">

Which is pretty much the same.
now on the processing page you can do:

ForEach($answers as $val) {
    Foreach ($val as $answer) {
        // do something with one of the answers
    }
}

Regards Michael

"Jule" <[EMAIL PROTECTED]> schrieb im Newsbeitrag
02051716404700.28871@localhost">news:02051716404700.28871@localhost...
Hey guys,
i'm getting this error whe i try to access this variable. $answers[answer$n]

Parse error: parse error, expecting `']'' in
/home/blindtheory/web/quiz/add_quiz/add_quiz_process_2.php on line 36

the variable $answer[answers$n] comes from a form on the preceding page in
which a number of answers has been entered. the number of answers is up to
the user and can vary from 2 to 15. not the $n comes from a for loop whcih
enteres the answers into a database since i do not know how many answers
each
user has used.

why am i getting this error?
and is there a way around it?
following is the for() loop in which this story takes place.

thanks
Jule

--SCRIPT--

for ($n = 1; $n <= $quiz[number_answers]; $n++) {
$table = "$quiz[code]_answers";
$value = "$answers[answer$n]";
$query_alter_table = "ALTER table $table ADD answer$n TEXT NUT NULL";
$query_add_answers = "INSERT INTO $table (answer$n) VALUES($value)";
if (mysql_db_query($database_glob, $query_alter_table, $link_glob) AND
(mysql_db_query($database_glob, $query_add_answer, $link_glob));
echo "Answer $n has successfully been added to the Quiz<br>\n";
} else {
echo mysql_error();
}
echo "Click here to continue";
}

--SCRIPT--

--
|\/\__________________________/\/|
|   Jule Slootbeek |
|   [EMAIL PROTECTED] |
|   http://blindtheory.cjb.net |
|   __________________________ |
|/\/       \/\|



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to