Table from 1 to 100 php nested in html

Hello,

i have a simple problem but it is giving me a hard time to find out the mistake am doing

i am trying to a create a table in php code implemented in an html table, using a for loop with if statement nested in it.

my code is :

<html>
	<head>
		<title>Ahmed</title>
	</head>
	<body>
		<table border="1">
			<?php
				for ($x=1; $x <= 100; $x++){
 				   if($x%2 ==0.5) {
					   echo "<tr><td>" . "<br>" . "</tr></td>";
 				  			}
								echo "<tc><td>" . $x . "</td></tc>" ;
									}
			?>
		</table>
	</body>
</html>

the table should break to a new row at 10, and 20 and 30 and so on, each number in a cell, my code got several flaws, first am getting a free empty cell after each row, and the breaking is happening in the wrong place ( which i assume is a mistake in the “if” statement ) i’ve tried the "if ($x%2 ==0.5) which i am sure should by right mathematically, but it gave me a a one row table without a break ?

could someone point out the problem and help me out here please ?

Try echoing $x%2 to see what values it returns

And if you want to break to a new row every 10 elements, shouldn’t you do something like

if ($x%10 == 0)

?

will i didnt try echoing $x%2 , but doing in a calculator it would always give me a number with a .5 except 2, 22, 24 … and the 10,20,30 and so so i thought it could work, but not really
plus originally what you suggested was my first choice which is ($x%10 ==0) but i kept getting this table in the picture i attached,

and it didnt break after 10 elements in the first row, it didn that in the following rows which is fine but not organized since it missed one cell in the first row and that missed up the whole serial of the table which should be something like this :

| 1 | 2 | … |10|
|11|12| …|20|
.
.
.
|91|92|…|100|

thank you for reply, i appreciate your help, but i hope my explanation gave you a better insight on what am trying to do ? whats the mistakes am doing though ?

Hi madnomad, welcome to the forums!

The code within your IF statement is the issue - you’re adding a row around a single table cell, when what you really want to be doing is wrapping every 10 cells in a row. Also, I’m not sure what the <tc> tags are for, as these aren’t valid tags as far as I know.

You can get the result you want by changing this section of your code:


<table border="1">
<?php
    foreach (range(1, 100) as $x)
    {
        if ($x % 10 == 1) {
            echo "<tr>";
        }

        echo "<td>$x</td>";

        if ($x % 10 == 0) {
            echo "</tr>";
        }
    }
?>
</table>

Every iteration it checks if dividing $x by 10 leaves a remainder of 1 (making it the first number in a new row) and if so, adds a <tr> tag before outputting the cell.

Thank you very much, i got it right, with the help of your code, but using it as it is, it helped me to understand my mistake, my code is :

<html>
	<head>
		<title>Ahmed</title>
	</head>
	<body>
		<table border="1">
			<?php
				for ($x=1; $x <= 100; $x++){
 				   if($x%10 ==1) {
					   echo "<tr>";
 				  			}
								echo "<td>" . $x . "</td>" ;
									}
			?>
		</table>
	</body>
</html>

this code will give you the same result as the code you posted, the difference is that i am using a “for” loop and an “if” statement, and the mistake was first the “<tc>” which is for creating a new column , but apparently i didnt need it, and since i was using that my first try with the if($x%10 ==1) didnt work i had to change up a bit and add the “<tc>” and got everything complected.

now i have a clear understanding of my code when i trace it, thanks to you explanation.

Thank you again.

No it doesn’t. Your code doesn’t have any </tr> tag

Some (all?) browsers will automatically add the closing row tag, but personally I’d rather explicitly add the tag myself.

True, my code is missing the </tr> closing tag, but apparently as “@fretburner” mentioned some browser will still accept the code as functional, and since it worked for me i guess that it true, but i appreciate the note.

Am using a Firefox btw and i tried it again without the closing tag and it is working, but fixed the code with the right construction, the missing closing tag, thanx again for the reminder.

i need to work on my tracing skills and not just depends on the browser view.