0
I need to write a script which can display an XHTML table with a numeric value displayed in each table cell. The script should use a nested loop construct and should be able, by changing the value of two variables (for example, $intWidth and $intHeight). to adjust the size of the table. This is what I have:
<?php

$i =1;

while($i<=8) {
    if($i%4==1) {
        echo "<tr>";
    }
    echo "<td>".$i."</td>";
    $i++;
    if($i%4==1) {
        echo "</tr>";
    }
}

?>

</table>
2
Contributors
4
Replies
25
Views
3 Years
Discussion Span
Last Post by burt.munn
0

<off-topic>
I noticed that you are creating new threads while others are on-going. This is not a crime, but if the other threads are solved, please mark them so:

http://www.daniweb.com/web-development/php/threads/475956/numeric-grades-to-letter-grades-in-php-and-htlm

http://www.daniweb.com/web-development/php/threads/475985/tables#post2078800

</off-topic>

The rows and cols are straightworward...

$rows = 10;
$cols = 9;

function createTable($rows,$cols)
{
    $i=1;
    $table='<table>';
    for($r=0;$r<$rows;$r++)
    {
        $table .= '<tr>';
        for($c=0;$c<$cols;$c++)
        {
            $table .= "<td>$i</td>";
            $i++;
        }
        $table .= '<tr>';
    }
    $table .= '</table>';
    return $table;
}

echo createTable($rows,$cols);

An example.

0

Another stupid question! can I ask if this could be done in something other than "function"?

0

What's wrong with a function? It's probably the best way to do it.
If you don't like it take off the function line and opening brace and the end closing brace and change 'return' to 'echo' and delete the last line. Done.

BTW - the other threads. Abandoned, Live, Solved?

Edited by diafol

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.