PDA

View Full Version : nested for loop


tony09uk
10-24-2012, 09:32 PM
I have an issue with a nested for loop. I have created a form that passes information to another page to echo out then put into a database. My problem is that the for loop on page two doesn't work how I expected it to work and after two hours of playing with alternative options ive given up.

On a previous page I have allowed the user to choose how many questions and how many choices for each question they want.

PAGE 1
echo "<h1>Change the values in each box</h1>\n";//title

echo "<form method=\"post\" action=\"addToDb.php\">\n";//start form

for($q = 1; $q <= $questions; $q++){//loop through number of questions required

echo " <input name='questions[]' type='text' value='question $q' /><br/>\n";

for($opt = 1; $opt <= $choices; $opt++){//loop through number of options required

echo " <input name='choice[]' type='text' value='option $opt' /><br/>\n";

}

}

echo " <input name='submit' type='submit' value='SUBMIT' />\n";//submit form
echo "</form>";//close form

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

foreach($q AS $v){
echo $v . "<br/>\n";
foreach($c AS $value){
echo " " . $value . "<br/>\n";
}


}

My problem is that the second for loop on page two seems to run through all the choices before moving on to question 2

EXAMPLE - what I want

Q1
C1
C2
C3
C4

Q2
C1
C2
C3
C4

Q3
C1
C2
C3
C4

EXAMPLE - what I GET

Q1
C1
C2
C3
C4
C1
C2
C3
C4
C1
C2
C3
C4

Q2
C1
C2
C3
C4
C1
C2
C3
C4
C1
C2
C3
C4

Q3
C1
C2
C3
C4
C1
C2
C3
C4
C1
C2
C3
C4

jmichae3
10-26-2012, 04:54 AM
the only reason I can explain this is that you have exactly 3 duplicates in choices[] somehow. check the code that generates your choices[]'s option elements.

tony09uk
10-27-2012, 12:48 PM
yeah i ran through the code several times and cant see anything wrong so im obviously missing something :(

tony09uk
11-05-2012, 12:14 PM
any other suggestions would be really helpful

jmichae3
11-06-2012, 05:21 AM
I can't see the entire page, so I can't really see anything right now.
read this page especially the comments
http://www.php.net/manual/en/language.variables.external.php

it appears you already have. I don't know what your input form elements look like (they may be broken). they should look like

<input type="checkbox" name="items[]" value="foo" />
<?php

what EXACTLY is your value for $choices? did you echo it? it's probably 12.
it's POSSIBLE your php.ini is set for old register_globals which converts $_REQUEST['question'] into $question.
if that's the case, it would cause your code some problems, so I would avoid coding it the way you have, try changing some variable names to avoid the conflict. it may vary with some servers if they are trying to allow old PHP code to execute nicely.

when I run the code wiht new php, I get this:
[code]
Mon 11/05/2012 22:07:30.57||E:\www\yahoo|>php temp.php
<input name='questions[]' type='text' value='question 1' /><br/>
<input name='choice[]' type='text' value='option 1' /><br/>
<input name='choice[]' type='text' value='option 2' /><br/>
<input name='choice[]' type='text' value='option 3' /><br/>
<input name='choice[]' type='text' value='option 4' /><br/>
<input name='questions[]' type='text' value='question 2' /><br/>
<input name='choice[]' type='text' value='option 1' /><br/>
<input name='choice[]' type='text' value='option 2' /><br/>
<input name='choice[]' type='text' value='option 3' /><br/>
<input name='choice[]' type='text' value='option 4' /><br/>
<input name='questions[]' type='text' value='question 3' /><br/>
<input name='choice[]' type='text' value='option 1' /><br/>
<input name='choice[]' type='text' value='option 2' /><br/>
<input name='choice[]' type='text' value='option 3' /><br/>
<input name='choice[]' type='text' value='option 4' /><br/>

jmichae3
11-06-2012, 05:22 AM
I initialized my code with $questions=3;$choices=4;

tony09uk
11-06-2012, 02:51 PM
jmichae3 thanks for your reply, I will take what you have said and have a further play and get back to you with what I achieve.