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 10-24-2012, 09:32 PM   #1
tony09uk
 
Join Date: Oct 2010
Posts: 340
Default nested for loop

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
PHP Code:
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
PHP Code:
    $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
tony09uk is offline   Reply With Quote
Old 10-26-2012, 04:54 AM   #2
jmichae3
 
Join Date: Dec 2010
Posts: 366
Default

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.
__________________
------------
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 10-27-2012, 12:48 PM   #3
tony09uk
 
Join Date: Oct 2010
Posts: 340
Default

yeah i ran through the code several times and cant see anything wrong so im obviously missing something
tony09uk is offline   Reply With Quote
Old 11-05-2012, 12:14 PM   #4
tony09uk
 
Join Date: Oct 2010
Posts: 340
Default

any other suggestions would be really helpful
tony09uk is offline   Reply With Quote
Old 11-06-2012, 05:21 AM   #5
jmichae3
 
Join Date: Dec 2010
Posts: 366
Default

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/languag...s.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
[code]
<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/>
__________________
------------
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 11-06-2012, 05:22 AM   #6
jmichae3
 
Join Date: Dec 2010
Posts: 366
Default

I initialized my code with
Code:
$questions=3;$choices=4;
__________________
------------
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 11-06-2012, 02:51 PM   #7
tony09uk
 
Join Date: Oct 2010
Posts: 340
Default

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.
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 07:12 PM.


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