Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

cell and row ids

Status
Not open for further replies.

SBuzzT

Programmer
Aug 24, 2005
86
CA
I am trying to write a loop that will write 10 rows with 6 cells in each row. I want to assign an id for each cell in increments of 1. I have been able to do so for 1 row, but am unsure how to proceed with the subsequent rows. Basically, I need the first row of 6 cells to be assigned ids 1 through 6 (sequentially). The next row would be assigned 7 though 12, etc. Here's what I had so far to write the cells in the first row.
Code:
<?
	for ($i=1; $i<7; $i++)
	{
		echo "<td id=\"td".$i."\">";
		echo "text</td>";
	}
?>
 
Use a different counter, for the row numbers. So that it keeps adding to itself every time.

The piece of code you posted will only output one row with 7 cells. Add another for loop around it for however many rws you want:

Code:
$cellnumber=0;
$rows=5;
for($j=0;$j<=$rows;$j++){
  for ($i=1; $i<7; $i++)
    {
        echo "<td id=\"td".$cellnumber."\">";
        echo "text</td>";
$cellnumber++;    
}

}

This will output 5 rows with 7 cells each, each with a different ID.


----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Opps forgot to add the <tr> and </tr> at the beggining and end of the outer loop.

Sorry.

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Here my code. It gave me 2 rows; the first had 6 cells. The second row output 24 cells. I think I missed something and I seem to be having a "dumb" day. The ids seem fine though...

Code:
<tr>
<?
$cellnumber=0;
$rows=3;
for($j=0;$j<=$rows;$j++)
{
  for ($i=1; $i<7; $i++)
    {
        echo "<td id=\"td".$cellnumber."\">";
        echo "text</td>";
				$cellnumber++;    
		}
}
?>
</tr>
 
Never mind... I think I have it.
Code:
$cellnumber=0;
$rows=3;
for($j=0;$j<=$rows;$j++)
{
  echo "<tr>";
  for ($i=1; $i<7; $i++)
    {
        echo "<td id=\"td".$cellnumber."\">";
        echo "text</td>";
				$cellnumber++;    
		}
	echo "</tr>";
}

Thanks a bunch!
 
put the <tr></tr> inside the for loop.

Code:
$cellnumber=0;
$rows=3;
for($j=0....){
   [red]echo "<tr>";[/red]
   for($i=...){
....
}
[red]echo "</tr>";[/red]
}

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
You welcome.

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top