ethos_bass Posted February 10, 2008 Share Posted February 10, 2008 I'm having a helluva time creating a table using a FOR loop. What I am shooting for is a table that has 7 columns and 6 rows (total of 42 cells). Here's the code I've come up with. <?php echo "<table border=\"2\" align=\"center\">"; //this opens the table tag for ($n=1;$n<=42;$n++) { $start_marker=($n - 1)/ 7; $end_marker=$n / 7; if ($n=1) // This places a <tr> tag at the beginning of the table { echo "<tr>"; } if (is_int($start_marker)) // This places a <tr> tag in every 7th cell, starting with #8. { echo "<tr>"; } echo "<td>$n</td>"; // This just prints the cell number in each cell if (is_int($end_marker)) { echo "</tr>"; // This adds the </tr> tag at the end of each row (or 7th cell starting with cell 7) } } echo "</table>"; // This closes the table. ?> So why doesn't it work? It seems like when I try to run the program it gets stuck in some kind of infinite loop and never actually sends results to my browser. Any ideas? Is there some subtle syntax guideline I'm violating? The output I'd like to see is: <table border="2" align="center"> <tr> <td>1</td> <td>2</td> <td>3</td> <td>4</td> <td>5</td> <td>6</td> <td>7</td> </tr> <tr> <td>8</td> . . . <td>42</td> </tr> <table> Any help would be greatly appreciated. Quote Link to comment Share on other sites More sharing options...
PHP Monkeh Posted February 10, 2008 Share Posted February 10, 2008 Would this not be a lot easier? echo "<table border=\"2\" align=\"center\">"; //this opens the table tag for($r=0; $r<6; $r++) { // This is the row For loop echo "<tr>" for($c=0; $c<7; $c++) { // This is the column For loop echo "<td> Row: ".$r." Column: ".$c."</td>"; } echo "</tr>"; } echo "</table>"; Within each of the row loops, it'll loop through and create the 7 columns. The text I put in each cell is so you can see which row + column is where. Quote Link to comment 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.