Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do a Diamond Pattern / Shape (asterisks) inside a table? (html + php)

I have to make a diamond-shaped asterisk using for loop, inside a table. It has to have "blank" <td> spaces before and after the asterisks to move and make it look centered, so it looks like a diamond. How do I do that? (I used PHP inside an HTML code.)

Code without the <tr> and <td> tags, it looked like a diamond because it was center aligned:

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

Code with the <tr> and <td> tags, need "spaces" for it to look like it's center aligned:

<?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>";
?>

Please help!

like image 822
Ken Flake Avatar asked Nov 25 '16 12:11

Ken Flake


2 Answers

Here is new Code with your solution. I have added logic to put blank td forward and backward to *

<?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>";
?>
like image 50
kevin Avatar answered Oct 25 '22 03:10

kevin


I used the code below without using a table to make a diamond shape.

<div style="text-align: center">
<?php
$n = 8;

if($n === 1){ die("input must be greater than 1"); }

$nn = ($n * 2);
$m = (ceil($nn / 2) + 1);
$temp = 0;

for($x = 1; $x <= $nn; $x++){
    $temp = (($x < $m) ? ($temp + 1) : ($temp - 1));
    $total = ($temp > 1 ? ((2 * $temp) - 1) : $temp);

    echo nl2br(str_repeat('* &nbsp;', $total) . "\r\n");
}
?>

like image 1
Mark Collamar Avatar answered Oct 25 '22 01:10

Mark Collamar