logo-dw

Go Back   Dreamweaver Club Forums > Hand Coders Forum > PHP
Register FAQ Members List Search Today's Posts Mark Forums Read

Reply
 
Thread Tools Display Modes
Old 12-02-2012, 08:48 PM   #1
tony09uk
 
Join Date: Oct 2010
Posts: 340
Default dynamically targeting multi-dimensional array

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:

PHP Code:
$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?
tony09uk is offline   Reply With Quote
Old 12-11-2012, 01:29 AM   #2
jmichae3
 
Join Date: Dec 2010
Posts: 366
Default

I remember this question. I have a better solution this time.
Code:
//$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
__________________
------------
Jim Michaels
HTML Code:
improperly<strong>nested<em>elements</strong>cause</em>
browser confusion (I believe the term is 'tag soup')!
jmichae3 is offline   Reply With Quote
Old 12-11-2012, 01:53 AM   #3
jmichae3
 
Join Date: Dec 2010
Posts: 366
Default

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
Code:
$_POST['question2']
you would have the
Code:
$_POST['choice2']
array. and within the same page you could have
Code:
$_POST['question1'][]
ahh skip it, I will just show you.

Code:
<!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>
__________________
------------
Jim Michaels
HTML Code:
improperly<strong>nested<em>elements</strong>cause</em>
browser confusion (I believe the term is 'tag soup')!
jmichae3 is offline   Reply With Quote
Old 12-11-2012, 01:54 AM   #4
jmichae3
 
Join Date: Dec 2010
Posts: 366
Default

it's output is
Code:
q1
C1A
C1B
C1C
C1D
C1E
q2
C2A
C2B
C2C
C2D
C2E
q3
C3A
C3B
C3C
C3D
C3E
q4
C4A
C4B
C4C
C4D
C4E
__________________
------------
Jim Michaels
HTML Code:
improperly<strong>nested<em>elements</strong>cause</em>
browser confusion (I believe the term is 'tag soup')!
jmichae3 is offline   Reply With Quote
Old 12-11-2012, 02:12 AM   #5
jmichae3
 
Join Date: Dec 2010
Posts: 366
Default

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.
__________________
------------
Jim Michaels
HTML Code:
improperly<strong>nested<em>elements</strong>cause</em>
browser confusion (I believe the term is 'tag soup')!
jmichae3 is offline   Reply With Quote
Old 12-13-2012, 02:33 PM   #6
tony09uk
 
Join Date: Oct 2010
Posts: 340
Default

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
tony09uk is offline   Reply With Quote
Old 12-14-2012, 05:20 AM   #7
jmichae3
 
Join Date: Dec 2010
Posts: 366
Default

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:
Code:
$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.
__________________
------------
Jim Michaels
HTML Code:
improperly<strong>nested<em>elements</strong>cause</em>
browser confusion (I believe the term is 'tag soup')!
jmichae3 is offline   Reply With Quote
Old 12-14-2012, 05:21 AM   #8
jmichae3
 
Join Date: Dec 2010
Posts: 366
Default

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.
__________________
------------
Jim Michaels
HTML Code:
improperly<strong>nested<em>elements</strong>cause</em>
browser confusion (I believe the term is 'tag soup')!
jmichae3 is offline   Reply With Quote
Old 12-14-2012, 10:59 PM   #9
tony09uk
 
Join Date: Oct 2010
Posts: 340
Default

It took a lot of looking but Im glad I stuck it out tahnks for your help
tony09uk is offline   Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 10:56 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Copyright 2006 DreamweaverClub.com