Jump to content

For Loop


jkewlo

Recommended Posts

hey this is retarted i have tired everything in the world i cant seem to figure it out

 

I am trying to make a table show

like this but I cant for the life of me

 

[][][][][][][][][][][][][][][][][][][][]

[][][][][][][][][][][][][][][][][][][][]

[][][][][][][][][][][][][][][][][][][][]

[][][][][][][][][][][][][][][][][][][][]

[][][][][][][][][][][][][][][][][][][][]

[][][][][][][][][][][][][][][][][][][][]

[][][][][][][][][][][][][][][][][][][][]

[][][][][][][][][][][][][][][][][][][][]

[][][][][][][][][][][][][][][][][][][][]

[][][][][][][][][][][][][][][][][][][][]

[][][][][][][][][][][][][][][][][][][][]

[][][][][][][][][][][][][][][][][][][][]

[][][][][][][][][][][][][][][][][][][][]

[][][][][][][][][][][][][][][][][][][][]

[][][][][][][][][][][][][][][][][][][][]

[][][][][][][][][][][][][][][][][][][][]

[][][][][][][][][][][][][][][][][][][][]

[][][][][][][][][][][][][][][][][][][][]

[][][][][][][][][][][][][][][][][][][][]

[][][][][][][][][][][][][][][][][][][][]

Link to comment
https://forums.phpfreaks.com/topic/185407-for-loop/
Share on other sites

$rows = 10;
$cols = 10;

for ($i = 0; $i < $rows; ++$i) {
    for ($j = 0; $j < $cols; ++$j) {
        echo '[]';
    }
    echo PHP_EOL;
}

// OR

for ($i = 0; $i < $rows; ++$i) {
    echo str_repeat('[]', $cols) . PHP_EOL;
}

// OR

for ($i = 0; $i < $rows * $cols; ++$i) {
    echo '[]';
    if ($rows % $cols == 0) {
        echo PHP_EOL;
    }
}

 

Neither of them tested, but the principles still apply.

Link to comment
https://forums.phpfreaks.com/topic/185407-for-loop/#findComment-978782
Share on other sites

Before the loops output an opening table tag, at the start of the row loop output a tr opening tag, at the start of the column loop output an opening td tag then your content then a closing td tag, after the column loop at the end of the row loop output a closing tv tag, after the row loop output a closing table tag.

Link to comment
https://forums.phpfreaks.com/topic/185407-for-loop/#findComment-978791
Share on other sites

Well, it's the same principle (you'd probably want to use the first approach). The inner for loop is for every cell, and the outer for loop does something on each row. Assuming you mean an HTML table, something like this:

 

<table>
<?php
$rows = 10;
$cols = 10;

for ($i = 0; $i < $rows; ++$i) {
    echo '<tr>';
    for ($j = 0; $j < $cols; ++$j) {
        echo "<td>{$i},{$j}</td>";
    }
    echo '</tr>';
}
?></table>

 

Again, untested.

Link to comment
https://forums.phpfreaks.com/topic/185407-for-loop/#findComment-978792
Share on other sites

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.