Jump to content

Creating Table with "FOR" statement


ethos_bass

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/90361-creating-table-with-for-statement/
Share on other sites

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.

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.