7 Replies - 4926 Views - Last Post: 21 October 2010 - 09:49 PM Rate Topic: -----

#1 Guest_AH104*


Reputation:

Problem with PHP Table - Nested For Loop

Posted 21 October 2010 - 09:21 PM

Hello everyone,

I'm new to PHP and am having what is probably a very simple problem with generating HTML tables using nested for loops. Basically, I am just trying to learn how to generate a table that lists numbers in sequential order. Something like this:

1 2 3 4 5 6 7
8 9 10 11 12 13 14
15, etc.

I know this is probably very simple, but I can't seem to figure it out! So far I have the following code:

	<?php
	
		echo "<table border='2'><br />";
		
			for ($row = 1; $row <= 6; $row ++) {
				echo "<tr>";
		
				for ($col = 1; $col <= 7; $col ++) {
					echo "<td>", ($col), "</td>";
				}
			echo "</tr>";
			}

		echo "</table>";
		
	?>




However, this only generates the following:

1 2 3 4 5 6 7
1 2 3 4 5 6 7
1 2 3 4 5 6 7
etc.

I'm trying to generate a table with 6 rows and 7 columns. Any help would be greatly appreciated!

Is This A Good Question/Topic? 0

Replies To: Problem with PHP Table - Nested For Loop

#2 Moshambi  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 8
  • View blog
  • Posts: 280
  • Joined: 20-November 07

Re: Problem with PHP Table - Nested For Loop

Posted 21 October 2010 - 09:33 PM

I wouldn't personally use 2 for loops in this case. I would just use an if statement and modulus like so:

<?php

	echo "<table border='2'><br />";
	echo "<tr>";
		for ($row = 1; $row <= 42; $row ++) {
			
			
			echo "<td>$row";
			
			if($row % 6 == 0)
			{
				echo "</td></tr><tr>";
				continue;
			}
			
				
		echo "</td>";
		
		
		}
	
	echo "</tr></table>";
	
?>




Hope this helps you out.


EDIT: Added continue statement so the table design is not off...

This post has been edited by Moshambi: 21 October 2010 - 09:36 PM

Was This Post Helpful? 0
  • +
  • -

#3 grimpirate  Icon User is offline

  • Pirate King
  • member icon

Reputation: 149
  • View blog
  • Posts: 714
  • Joined: 03-August 06

Re: Problem with PHP Table - Nested For Loop

Posted 21 October 2010 - 09:36 PM

Think about multiplying the row value by something and then adding that the column value.
Was This Post Helpful? 0
  • +
  • -

#4 Guest_AH104*


Reputation:

Re: Problem with PHP Table - Nested For Loop

Posted 21 October 2010 - 09:39 PM

Thanks for the quick response! That is certainly an efficient way to do it, I hadn't even though of using an if statement. Just for my own curiosity though, is there an effective way to do the same thing with a nested for loop? I've been trying to figure it out for hours! Thanks so much for your help.
Was This Post Helpful? 0

#5 Guest_AH104*


Reputation:

Re: Problem with PHP Table - Nested For Loop

Posted 21 October 2010 - 09:41 PM

View Postgrimpirate, on 21 October 2010 - 08:36 PM, said:

Think about multiplying the row value by something and then adding that the column value.


Thanks for the tip grimpirate. This can't be that difficult, I will continue my attempts to figure it out. I really appreciate the quick responses, what a great forum!
Was This Post Helpful? 0

#6 Moshambi  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 8
  • View blog
  • Posts: 280
  • Joined: 20-November 07

Re: Problem with PHP Table - Nested For Loop

Posted 21 October 2010 - 09:43 PM

If you must do it with nested for loops then this is the way I would do it:

<?php

	echo "<table border='2'><br />";
	
		for ($row = 0; $row < 6; $row ++) {
			echo "<tr>";
	
			for ($col = 1; $col <= 7; $col ++) {
				echo "<td>", ($col + ($row * 7)), "</td>";
			}
		echo "</tr>";
		}

	echo "</table>";
	
?>


Was This Post Helpful? 0
  • +
  • -

#7 Guest_AH104*


Reputation:

Re: Problem with PHP Table - Nested For Loop

Posted 21 October 2010 - 09:46 PM

Doh! Of course, it seems so obvious now. Ugh, sometimes one makes things much more complicated than they need to be, while the answer was staring them right in the face! Thanks everyone for all your help, I really appreciate it.
Was This Post Helpful? 0

#8 Moshambi  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 8
  • View blog
  • Posts: 280
  • Joined: 20-November 07

Re: Problem with PHP Table - Nested For Loop

Posted 21 October 2010 - 09:49 PM

No problem. I know how code can get the best of you sometimes especially when you are staring it in the face for hours on end...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1