RE: [PHP-WIN] multi-dimensional array
| From: | Ignatius Teo | Date: | Wed, 15 Nov 2000 04:01:14 +0000 |
| Subject: | RE: [PHP-WIN] multi-dimensional array | ||
| References: | 1 | Groups: | php.windows |
| Request: | Send a blank email to php-windows+get-3849@lists.php.net to get a copy of this message | ||
afan,
No, you can't use FORM to create arrays...but you could use PHP to generate the arrays instead
:-)
Basically, you can use an array (or database) to store your questions and answers. Then use PHP to
dynamically generate the form variables...which is what I assume you are trying to do?
You can do it like so:
<?php
if (isset($HTTP_POST_VARS)) {
// form was posted...let's see what we got back
print "<PRE>\n";
print_r($HTTP_POST_VARS);
print "</PRE>\n";
// this is where you process your form variables
} else {
// OK, let's set up some questions...
$ary_Questions = array("Select some colors","Select some options","Select
some more options");
// ...and answers
$ary_Answers[0] =
array("blue","green","red","yellow","purple");
$ary_Answers[1] = array("Yes","No","Maybe");
$ary_Answers[2] = array("Sometimes","All the time","Not at all");
// righto! let's print the form out
?>
<FORM ACTION="<?php echo basename($PHP_SELF);?>" METHOD="POST">
<?php
// for each question
while (list($i,$question)=each($ary_Questions)) {
// print the question
print "Q.$i $question: ";
// OK, now print the responses for each question
while (list($j,$answer)=each($ary_Answers[$i])) {
// ooh...here's the trick!
// print the form var as a 2-D array
// you can then use $Response[$question][$answer] to reference the returned variables
print "<INPUT type=checkbox name=Response[".$i."][".$j."]
value=\"$answer\">$answer ";
}
print "<BR>";
}
// we're done with the hard bit, let's close the form
?>
<BR><INPUT type=submit name=submit value="Submit">
</FORM>
<?php
}
?>
Hope this helps....
Ignatius Teo
G2 Project Manager
Web Development & Online Technologies Group
Snail: Information Technology Directorate
Southern Cross University
PO Box 157, Lismore NSW 2480
Voice: +61 2 6620-3221
Fax : +61 2 6620-3033
Email:iteo@scu.edu.au
> -----Original Message-----
> From: afan [mailto:afan@sprout.net]
> Sent: Wednesday, 15 November 2000 08:27
> To: php-windows@lists.php.net
> Subject: [PHP-WIN] multi-dimensional array
>
>
> HI All!
>
> Here is my problem.
>
> I have to create a little survey with 5 questions. Each
> question has 2, 3, 4 or 5 answers/possibilities but, correct
> answer can be more than one possibility. That means, instead
> of radio buttons I have to use checkbox buttons.
>
> Idea is to work with arrays:
> $nop = array(1=>2, 4, 3, 2, 5) - number of answers for 5
> questions, first question has 2 answers, second - 4, third - 3...
> $Q = array(1=>q1, q2, q3, q4, q5) - array of questions
> $A1 = array(1=>a1, a2) - according $nop, only 2
> answers/possibilities
> $A2 = array(1=>a1, a2, a3, a4)
> $A3 = array(1=>a1, a2, a3)
> $A4 = array(1=>a1, a2)
> $A5 = array(1=>a1, a2, a3, a4, a5)
> $A =array(1=> $A1, $A2, $A3, $A4, $A5) - two-dimensional array
>
> How can I use FORM to create those arrays?
> When I have 5 arrays - how can I create new array, made of
> those 5 arrays (two-dimensional array)?
>
>
>