如何在桌子内做钻石图案/形状(星号)?(html + php)

Ken*_*ake 5 html php html-table

我必须在桌子内使用for循环制作菱形星号.它必须<td>在星号之前和之后有"空白" 空间才能移动并使其看起来居中,因此它看起来像钻石.我怎么做?(我在HTML代码中使用了PHP.)

没有<tr><td>标签的代码,它看起来像钻石,因为它是中心对齐的:

<center>
<?php
echo "<table border = 1>";

    // loop for the pyramid

        for($i = 1; $i <= 10; $i += 2) {
            for($j = 1; $j <= $i; $j++) {
                echo "* ";
            }
        echo "<br />";
        }

    // loop for the inverted pyramid, so it looks like a diamond

    for($i = 7; $i >= 1; $i -= 2) {
        for($j = 1; $j <= $i; $j++) {
            echo "* ";
        }   
    echo "<br />";
    }

echo "</table>";
?>
</center>
Run Code Online (Sandbox Code Playgroud)

带有<tr><td>标签的代码,需要"空格"才能看起来像是中心对齐的:

<?php
echo "<table border = 1>";

    // loop for the pyramid

    echo "<tr>";
            for($i = 1; $i <= 10; $i += 2) {
            echo "<tr>";
                for($j = 1; $j <= $i; $j++) {
                    echo "<td>* </td>";
                }
            echo "</tr>";
        }
    echo "</tr>";

    // loop for the inverted pyramid, so it looks like a diamond

    for($i = 7; $i >= 1; $i -= 2) {
    echo "<tr>";
        for($j = 1; $j <= $i; $j++) {
            echo "<td>* </td>";
        }   
    echo "<br />";
    echo "</tr>";
    }

echo "</table>";
?>
Run Code Online (Sandbox Code Playgroud)

请帮忙!

kev*_*vin 4

这是包含您的解决方案的新代码。我添加了逻辑将空白 td 向前和向后放置到 *

<?php
echo "<table border = 1>";

    // loop for the pyramid

    echo "<tr>";
    $max = $initAmount = 10;
            for($i = 1; $i <= $initAmount; $i += 2) {
                $max = $max -2;

                    $halfTD = (int)$max/2;
            echo "<tr>";
            for($b = 1; $b <= $halfTD; $b++){
                        echo "<td></td>";
                    }
                for($j = 1; $j <= $i; $j++) {


                    echo "<td>* </td>";
                }
                for($b = 1; $b <= $halfTD; $b++){
                        echo "<td></td>";
                    }
            echo "</tr>";
        }
    echo "</tr>";

    // loop for the inverted pyramid, so it looks like a diamond
$max = $initAmount = 10;
    for($i = 7; $i >= 1; $i -= 2) {
        $max = $max -2;
        $diff = $initAmount - $max;
        $blankTd = $diff/2;

    echo "<tr>";
        for($b = 1 ; $b <= $blankTd; $b++){
            echo "<td></td>";
        }
        for($j = 1; $j <= $i; $j++) {
            echo "<td>* </td>";
        }   
        for($b = 1 ; $b <= $blankTd; $b++){
            echo "<td></td>";
        }
    echo "</tr>";
    }

echo "</table>";
?>
Run Code Online (Sandbox Code Playgroud)