PDA

View Full Version : dynamically targeting multi-dimensional array


tony09uk
12-02-2012, 08:48 PM
I have a multi-dimensional array which is being populated via the use of a form, I am trying to output elements of that form, so I need a way to target the elements of the array dynamically.

currently I am doing this:


$q = $_POST['questions'];
$c = $_POST['choice'];

foreach($q AS $v){

echo $v . '<br/>';
echo $c['1-1'][0] . '<br/>';

for ($i = 0; $i <= $count; $i++){
echo next($c['1-1']) . '<br/>';
}

}


PROBLEM:

This script outputs the $q[0], then the first set of choices that are linked to that question. It then outputs question two BUT repeats the first set of choices again, as it does for however many questions have been requested.

SOLUTION REQUIRED:
A way of targeting the ['1-1'] in the $c variable dynamically.

Unless there is a better way?

jmichae3
12-11-2012, 01:29 AM
I remember this question. I have a better solution this time.

//$q = $_POST['questions']; //array of questions
//$c = $_POST['choice']; //array of questions
if (isset($_POST['questions']) && isset($_POST['choice'])) {
foreach($_POST['questions'] as $qkey=>$qval) {
echo $qval . '<br/>';
foreach ($_POST['choice'] as $ckey=>$cval) {
echo $cval . '<br/>';
}
}
}

qa
CA
CB
CC
CD
CE
qb
CA
CB
CC
CD
CE
qc
CA
CB
CC
CD
CE
qd
CA
CB
CC
CD
CE

jmichae3
12-11-2012, 01:53 AM
can you be a little more explicit about what you are trying to achieve? I tried (and failed because of the complexity of the database structure) to make my own online quiz thing. then I found such a one exists called moodle. are you trying to have multiple questions and multiple answers on one page? because I think you are attacking the problem just from looking at your forms, all the wrong way. when it comes time to enumerate the form results, you have them all merged and there is no way to differentiate the choices from one question from the choices in another question. so you need to correspondingly number your choices with your questions.
have one hidden variable with the number of questions. or you can simply get the count() of the number of questions. once you have that number,

use that as a maximum count for a for loop from 1 to that maximum.
you simply append that number to the end of 'question' and 'choice' to get the questions and choices for a given question.

alternatively, you can use any code you like in place of a number for selecting a choice array, possibly extracted from the question.

does this help?

for instance, for $_POST['question2'] you would have the $_POST['choice2'] array. and within the same page you could have $_POST['question1'][] ahh skip it, I will just show you.


<!DOCTYLE html>
<html>
<head>
<title>tok09uk problem</title>
</head>
<body>
<form name="z" action="temp.php" method="post">
<input type="hidden" name="numquestions" value="4">
<input type='text' value='q1' name=question1>
<input type='text' value='C1A' name=choice1[]>
<input type='text' value='C1B' name=choice1[]>
<input type='text' value='C1C' name=choice1[]>
<input type='text' value='C1D' name=choice1[]>
<input type='text' value='C1E' name=choice1[]>
<hr>
<input type='text' value='q2' name=question2>
<input type='text' value='C2A' name=choice2[]>
<input type='text' value='C2B' name=choice2[]>
<input type='text' value='C2C' name=choice2[]>
<input type='text' value='C2D' name=choice2[]>
<input type='text' value='C2E' name=choice2[]>
<hr>
<input type='text' value='q3' name=question3>
<input type='text' value='C3A' name=choice3[]>
<input type='text' value='C3B' name=choice3[]>
<input type='text' value='C3C' name=choice3[]>
<input type='text' value='C3D' name=choice3[]>
<input type='text' value='C3E' name=choice3[]>
<hr>
<input type='text' value='q4' name=question4>
<input type='text' value='C4A' name=choice4[]>
<input type='text' value='C4B' name=choice4[]>
<input type='text' value='C4C' name=choice4[]>
<input type='text' value='C4D' name=choice4[]>
<input type='text' value='C4E' name=choice4[]>
<hr>

<input type="submit">
</form>
<pre>
<?php
//$q = $_POST['questions']; //array of questions
//$c = $_POST['choice']; //array of questions
if (isset($_POST['numquestions'])) {
$nq = intval($_POST['numquestions']); //prevents hacking
if ($nq >= 1) {
for($qi=1; $qi <= $nq; $qi++) {
echo $_POST['question'.$qi] . '<br/>';
foreach ($_POST['choice'.$qi] as $ckey=>$cval) {
echo $cval . '<br/>';
}
}
}
}


?>
</pre>



</body>
</html>

jmichae3
12-11-2012, 01:54 AM
it's output is
q1
C1A
C1B
C1C
C1D
C1E
q2
C2A
C2B
C2C
C2D
C2E
q3
C3A
C3B
C3C
C3D
C3E
q4
C4A
C4B
C4C
C4D
C4E

jmichae3
12-11-2012, 02:12 AM
the input element arrays are 1-dimensional. I hope this answered your question. if I knew what kind of page content you were trying to make, I could help you out by engineering you a solution.

tony09uk
12-13-2012, 02:33 PM
hi since posting this question, I have found that a multi dimensionall array is required, as you have suggested. Thanks for your help. I am sure I will be back with more questions on this subject :D

jmichae3
12-14-2012, 05:20 AM
I was beginning to think you were wanting a multidimensional array on your POST... :-)

there are ways to do this, if you use codes. for instance, you can put a tail on the input elements' names with underscores between to separate them, to represent the nesting of indexes, like this:


choices_7_3_5_99

does this give you any ideas?

it's the way you encode things that allows you to simulate multidimensional arrays with a 1-dimensional array. watch this:


$imax=5;$jmax=8;$kmax=3;
echo "<input type=hidden name=imax value=$imax>\n";
echo "<input type=hidden name=jmax value=$jmax>\n";
echo "<input type=hidden name=kmax value=$kmax>\n";
for ($i=1; $i <= $imax; $i++) {
for ($j=1; $j <= $jmax; $j++) {
for ($k=1; $k <= $kmax; $k++) {
echo "<input type=text name=".$i."_".$j."_".$k.">\n";
}
}
}

this is only one way of doing it, if you wanted to use only 1 choice array.

jmichae3
12-14-2012, 05:21 AM
you can use words if you want in place of numbers using foreach, but then you have to use count() on an array of words to get a count, and you have to have 3 individual arrays of words.

tony09uk
12-14-2012, 10:59 PM
It took a lot of looking but Im glad I stuck it out :D tahnks for your help