user14691343
user14691343

Reputation:

Generate a pyramid using PHP For Loop and HTML

I made a script where it creates a pyramid using a php for loop and html table elements, but the pyramid isn't how I want it yet. The code:

<?php
echo "<table width=400px";
echo "<tr>";
//Inner Loop
for ($x = 0; $x <= 8; $x++) {
//Outer loop
    for ($z = 1; $z <= $x; $z++) {
        echo "<td height=50px width=50px bgcolor=black></td>";
    }
    echo "\n";
    echo "</tr>";
}
echo "</table>";
?>

Right now it looks like this: enter image description here

But I want it to look like this: enter image description here

Can someone help me with that?

Upvotes: 0

Views: 913

Answers (1)

JS KIM
JS KIM

Reputation: 695

Simply add cellspacing=0 to table

<?php
echo "<table cellspacing=0 width=400px";
echo "<tr>";
//Inner Loop
for ($x = 0; $x <= 8; $x++) {
//Outer loop
    for ($z = 1; $z <= $x; $z++) {
        echo "<td height=50px width=50px bgcolor=black></td>";
    }
    echo "\n";
    echo "</tr>";
}
echo "</table>";
?>

Upvotes: 1

Related Questions