ToddAtWSU Posted March 4, 2007 Share Posted March 4, 2007 I am trying to improve my code so it is smaller and more dynamic. Right now it allows me to only have 16 teams on my page. The code to construct my table looks like: <table border="0" align="center"> <tr> <td align="right"> <?php global $team; echo $team[0]; ?> </td> <td><input type="text" name="visitorOne" size=2 /></td> <td><input type="text" name="homeOne" size=2 /></td> <td> <?php global $team; echo $team[1]; ?> </td> </tr> <tr> <td align="right"> <?php global $team; echo $team[2]; ?> </td> <td><input type="text" name="visitorTwo" size=2 /></td> <td><input type="text" name="homeTwo" size=2 /></td> <td> <?php global $team; echo $team[3]; ?> </td> </tr> <tr> <td align="right"> <?php global $team; echo $team[4]; ?> </td> <td><input type="text" name="visitorThree" size=2 /></td> <td><input type="text" name="homeThree" size=2 /></td> <td> <?php global $team; echo $team[5]; ?> </td> </tr> <tr> <td align="right"> <?php global $team; echo $team[6]; ?> </td> <td><input type="text" name="visitorFour" size=2 /></td> <td><input type="text" name="homeFour" size=2 /></td> <td> <?php global $team; echo $team[7]; ?> </td> </tr> <tr> <td align="right"> <?php global $team; echo $team[8]; ?> </td> <td><input type="text" name="visitorFive" size=2 /></td> <td><input type="text" name="homeFive" size=2 /></td> <td> <?php global $team; echo $team[9]; ?> </td> </tr> <tr> <td align="right"> <?php global $team; echo $team[10]; ?> </td> <td><input type="text" name="visitorSix" size=2 /></td> <td><input type="text" name="homeSix" size=2 /></td> <td> <?php global $team; echo $team[11]; ?> </td> </tr> <tr> <td align="right"> <?php global $team; echo $team[12]; ?> </td> <td><input type="text" name="visitorSeven" size=2 /></td> <td><input type="text" name="homeSeven" size=2 /></td> <td> <?php global $team; echo $team[13]; ?> </td> </tr> <tr> <td align="right"> <?php global $team; echo $team[14]; ?> </td> <td><input type="text" name="visitorEight" size=2 /></td> <td><input type="text" name="homeEight" size=2 /></td> <td> <?php global $team; echo $team[15]; ?> </td> </tr> </table> this was my attempt to make it more dynamic but I already see a problem arising. <table border="0" align="center"> <?php global $team; $gameNumber = 1; for( $i = 0 ; $i < count( $team ) ; $i++ ) { echo "<tr>"; echo "<td align=\"right\">"; echo $team[$i]; echo "</td>"; echo "<td><input type=\"text\" name=\"visitorOne\" size=2 /></td>"; echo "<td><input type=\"text\" name=\"homeOne\" size=2 /></td>"; $i++; echo "<td align=\"left\">"; echo $team[$i]; echo "</td>"; echo "</tr>"; } ?> </table> The problem I see is when I create the inputs of type="text" with their names. The first row names should be visitorOne and homeOne and then the next row should be visitorTwo and homeTwo and so on. Is there a way to dynamically create my input names so I can successfully use them later on with the $_POST argument in another page that receives the Submit action? Thanks again for your help! Quote Link to comment https://forums.phpfreaks.com/topic/41144-solved-dynamic-for-loop/ Share on other sites More sharing options...
Orio Posted March 4, 2007 Share Posted March 4, 2007 I think this should work: <?php global $team; $j = 0; $word = array("One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight"); for( $i = 0 ; $i < count($team) -1 ; $i++ ) { if($i % 2 == 0) { echo "<tr>"; echo "<td align=\"right\">"; echo $team[$i]; echo "</td>"; echo "<td><input type=\"text\" name=\"visitor".$word[$j]."\" size=2 /></td>"; echo "<td><input type=\"text\" name=\"home".$word[$j]."\" size=2 /></td>"; $j++; } else { echo "<td align=\"left\">"; echo $team[$i]; echo "</td>"; echo "</tr>"; } ?> Orio. Quote Link to comment https://forums.phpfreaks.com/topic/41144-solved-dynamic-for-loop/#findComment-199299 Share on other sites More sharing options...
ToddAtWSU Posted March 4, 2007 Author Share Posted March 4, 2007 The one problem I see with your solution is it still binds me to a static amount by setting array to "One" "Two" ... "Eight". What I am thinking is creating a loop like this: $gameNumber; for( $a = 1 ; $a < ( count( $team ) / 2 + 1 ) ; $a++ ) { $gameNumber[$a-1] = (string) $a; } My problem is the (string) $a returns "1" instead on "one" or "One". Is there a way to dynamically do this? Thanks again! Quote Link to comment https://forums.phpfreaks.com/topic/41144-solved-dynamic-for-loop/#findComment-199325 Share on other sites More sharing options...
ToddAtWSU Posted March 4, 2007 Author Share Posted March 4, 2007 I guess HTML <input> names can have numbers in it so visitor1 and home2 works fine. Thanks for your help! Quote Link to comment https://forums.phpfreaks.com/topic/41144-solved-dynamic-for-loop/#findComment-199445 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.