Loops to create a table

Hello,
I am working on a tutorial for my PHP class and am having trouble getting this code to work. I have tried multiple things but the page continues to display an error about an unexpected ‘)’ expecting’;’. I’m sure it’s a simple mistake but if someone could help me out with this I would greatly appreciate it.

[php]

<?php $varColumns = 5; $varRows = 10; ?> Table with loops <?php for ($i=1, $i<=$varRows, $i++) { echo "\n"; for ($j=1, $j <= $varColumns, $j++) { echo " \n"; } echo "\n"; } ?>
 
[/php]

Firstly, you don’t need the \n for newlines. Also your for loop syntax is wrong.

[php]
for (init; condition; increment)
{
code to be executed;
}
[/php]

In this case your loop should be

[php]

<?php for ($i=1; $i<=$varRows; $i++) { echo ""; for ($j=1; $j <= $varColumns; $j++) { echo "  "; } echo ""; } ?>

[/php]

If whatever tutorial you’re following told you to use the syntax like you had it, it needs fixing majorly.

If he want to loop the code for certain number of times if $i value is between 1 to 7, he can use my solution. I’m assuming that he don’t want to touch the value of $i inside the loop. For example if he want to loop code for 5 times if $i value is between 1 to 7.

Yes, if he just want to loop 1 to 7 your code is perfect.

Sponsor our Newsletter | Privacy Policy | Terms of Service