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>

Recommended Answers

All 4 Replies

Member Avatar for diafol

<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.

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

Member Avatar for diafol

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?

Thank You very Much!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.