Sleeper Posted April 1, 2008 Share Posted April 1, 2008 I have a script im building and I am explorering the loop option for the first time. now I have 3 differnt fields that will need to loop such as $a1 -$b1 -$c1 that I want to loop to $a2 $b2 $c2 and so on down the rows. Now I can do the for ( $counter = 1; $counter <= 40; $counter += 1) to get the numbers from 1-40 or what ever I set there but how to I get the echo to add $a to the 1,2,3.....? I though I could do echo "<input type=\"text\" value='$a$counter' name=\"a$counter\" size=\"10\">"; but the $a is not a valid optoin so it skips over it. So how do I get those to wright as $a1 and pull the value for $a1 and keep looping? Quote Link to comment https://forums.phpfreaks.com/topic/99073-loop-help/ Share on other sites More sharing options...
MadTechie Posted April 1, 2008 Share Posted April 1, 2008 why not use an array ? ie echo "<input type=\"text\" value='a[]' name=\"a[]\" size=\"10\">"; then print_r($_POST['a']); Quote Link to comment https://forums.phpfreaks.com/topic/99073-loop-help/#findComment-506947 Share on other sites More sharing options...
Sleeper Posted April 1, 2008 Author Share Posted April 1, 2008 Cause ive never used arrays before Im still learning here...lol but Im not sure how that dose the job... here is the full coding I have now with your [] in there and its erroring... <?php include ("inc/config.php"); include ("inc/data_djnames.php"); print_r($_POST['djnamefc']); echo "<table border=\"2\" align=\"center\" width=\"383\" bordercolor=\"#000000\" >"; echo "<tr><th>DJ Name</th>"; echo "<th>DJ#</th>"; echo "<th>DJ Color</th></tr>"; for ( $counter = 1; $counter <= 40; $counter += 1) { echo "<tr><td>"; echo $counter; echo "</td><td width=78>"; echo "<font class=\"djname2\">DJ #$counter</font>"; echo "</td><td width=78 >"; echo "<input type=\"text\" value='$djnamefc[]' name=\"djnamefc$counter\" size=\"10\">"; echo "</td></tr>"; } echo "</table>"; ?> and im getting Parse error: syntax error, unexpected ']', expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/hookah17/public_html/timesheet/admin/test.php on line 17 Quote Link to comment https://forums.phpfreaks.com/topic/99073-loop-help/#findComment-506953 Share on other sites More sharing options...
MadTechie Posted April 1, 2008 Share Posted April 1, 2008 your need to escape the brackets (square brackets) try this (untested) <?php include ("inc/config.php"); include ("inc/data_djnames.php"); print_r($_POST['djnamefc']); echo "<table border=\"2\" align=\"center\" width=\"383\" bordercolor=\"#000000\" >"; echo "<tr><th>DJ Name</th>"; echo "<th>DJ#</th>"; echo "<th>DJ Color</th></tr>"; for ( $counter = 1; $counter <= 40; $counter += 1) { echo "<tr><td>"; echo $counter; echo "</td><td width=78>"; echo "<font class=\"djname2\">DJ #$counter</font>"; echo "</td><td width=78 >"; echo '<input type="text" value="'.$_POST['djnamefc'][$counter].'" name="djnamefc['.$counter.']" size="10">'; echo "</td></tr>"; } echo "</table>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/99073-loop-help/#findComment-506963 Share on other sites More sharing options...
Barand Posted April 1, 2008 Share Posted April 1, 2008 do you mean <?php $arr = array('fname','lname','email'); for ($row=1; $row<=9; $row++) { for ($col=0; $col < 3; $col++) { echo "<input type='text' name='{$arr[$col]}[$row]' value='{$arr[$col]}$row'> \n"; } echo "\n<br/>\n"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/99073-loop-help/#findComment-506968 Share on other sites More sharing options...
Sleeper Posted April 1, 2008 Author Share Posted April 1, 2008 ok that ends up giving a value="" See the data_djnames.php will have the color value of $djnamefc1, $djnamefc2 and so on. So I just need the value="$djnamefc#" to be able to pull in the info so I need to be able to do have the vaule = $djnamefc1, $djnamefc2 and so on. Quote Link to comment https://forums.phpfreaks.com/topic/99073-loop-help/#findComment-506970 Share on other sites More sharing options...
MadTechie Posted April 2, 2008 Share Posted April 2, 2008 the code i posted to should call the values like so echo $_POST['djnamefc'][1]; echo $_POST['djnamefc'][2]; etc but i'll leave you in Barand wise hands, i'm off to bed .... need sleep Quote Link to comment https://forums.phpfreaks.com/topic/99073-loop-help/#findComment-506973 Share on other sites More sharing options...
Sleeper Posted April 2, 2008 Author Share Posted April 2, 2008 ok if I go back and make all my values to $djnamefc[1] then I can do echo "<input type=text value='$djnamefc[$counter]' name='djnamefc$counter' size=10>"; And its pulls up the info. is this the only way? cause if so I hve to go back and change all the coding for every one of those var's Quote Link to comment https://forums.phpfreaks.com/topic/99073-loop-help/#findComment-506983 Share on other sites More sharing options...
Sleeper Posted April 2, 2008 Author Share Posted April 2, 2008 Nevermind that will work just fine. Thanks for the help. Quote Link to comment https://forums.phpfreaks.com/topic/99073-loop-help/#findComment-507098 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.