Perad Posted November 24, 2006 Share Posted November 24, 2006 [code]for ($i=0;$i<$pn;$i++) { echo $pn1 . ' - ' . $pn2; echo '<input type="text" name="player'.($i+1).'" size="30" maxlength="50" value="'.$pn.($i+1).'" />'; echo '<input type="text" name="playerscore'.($i+1).'" size="3" maxlength="3" value="'.$ps.($i+1).'" />'; }[/code]I'm baffled, the $pn and $ps variables are defined in an external file, all variables are correct and work.echo $pn1 . ' - ' . $pn2;generates the correct values yet the form is returning incorrect values. No matter what edit page i flip to it comes up with exactly the same values and variables. Any ideas? Link to comment https://forums.phpfreaks.com/topic/28321-why-doesnt-this-loop-work/ Share on other sites More sharing options...
heckenschutze Posted November 24, 2006 Share Posted November 24, 2006 Are $pn and $ps in scope? Link to comment https://forums.phpfreaks.com/topic/28321-why-doesnt-this-loop-work/#findComment-129525 Share on other sites More sharing options...
Perad Posted November 24, 2006 Author Share Posted November 24, 2006 They must be because the pn echo's ok from within the loop, its only when i try to loop it out withe $i that i have problems.Is this part correct?[code]value="'.$pn.($i+1).'"[/code]It should generate $pn1, $pn2 etc Link to comment https://forums.phpfreaks.com/topic/28321-why-doesnt-this-loop-work/#findComment-129530 Share on other sites More sharing options...
joshi_v Posted November 24, 2006 Share Posted November 24, 2006 Replace it withvalue="$pn.$i+1"RegardsJoshi Link to comment https://forums.phpfreaks.com/topic/28321-why-doesnt-this-loop-work/#findComment-129552 Share on other sites More sharing options...
kenrbnsn Posted November 24, 2006 Share Posted November 24, 2006 You really should use arrays here. They are much easier to use than trying to generate variables with the names like $x1, $x2, etc.[code]<?phpfor ($i=1;$i<=count($pn);$i++) { echo '<input type="text" name="player['.$i.']" size="30" maxlength="50" value="'.$pn[$i].'" />'; echo '<input type="text" name="playerscore['.$i.']" size="3" maxlength="3" value="'.$ps[$i].'" />';}?>[/code]You will have to modify the rest of your script to use arrays instead of the generated variable names. This may take some time, but it will make your life much easier.Ken Link to comment https://forums.phpfreaks.com/topic/28321-why-doesnt-this-loop-work/#findComment-129577 Share on other sites More sharing options...
Perad Posted November 24, 2006 Author Share Posted November 24, 2006 OK i will have a crack at turning it into an array, i think i know how to do it, could you just help with the first step please.These are my variables which define where to get the data from.[code]$ps1 = ($row['score1']);$ps2 = ($row['score2']);$ps3 = ($row['score3']);$ps4 = ($row['score4']);$ps5 = ($row['score5']);$ps6 = ($row['score6']);$ps7 = ($row['score7']);$ps8 = ($row['score8']);$ps9 = ($row['score9']);$ps10 = ($row['score10']);$ps11 = ($row['score11']);$ps12 = ($row['score12']);$ps13 = ($row['score13']);$ps14 = ($row['score14']);$ps15 = ($row['score15']);$ps16 = ($row['score16']);$ps17 = ($row['score17']);$ps18 = ($row['score18']);$ps19 = ($row['score19']);$ps20 = ($row['score20']);$ps21 = ($row['score21']);$ps22 = ($row['score22']);$ps23 = ($row['score23']);$ps24 = ($row['score24']);[/code]Is there a way to put this into an array than$array = array(($row['score2']), ($row['score2']), etc etc Link to comment https://forums.phpfreaks.com/topic/28321-why-doesnt-this-loop-work/#findComment-129598 Share on other sites More sharing options...
kenrbnsn Posted November 24, 2006 Share Posted November 24, 2006 Where is the $row array coming from?If you can't change that into a two dimentional array, you can use a for loop such as:[code]<?php$ps = array();for ($i=1;$i<25;$i++) $ps[$i] = $row['score' . $i];?>[/code]If I were doing this, I would first change the $row array into another array:[code]<?php$row2 = array();$ps = array();for ($i=1;$i<25;$i++) { $row2['score'][$i] = $row['score' . $i]; $ps[$i] = $row2['score'][$i]}?>[/code]Ken Link to comment https://forums.phpfreaks.com/topic/28321-why-doesnt-this-loop-work/#findComment-129602 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.