How to make a diamond pattern / shape (stars) inside the table? (html + php)

I have to make a star-shaped star using for the loop inside the table. It must have <td> spaces before and after the stars to move around and make them centered , so it looks like a diamond. How can I do it? (I used PHP inside the HTML code.)

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

 <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 tags <tr> and <td> , for this you need "spaces" to make it look like aligned in the center:

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

+5
source share
4 answers

Here is the new code with your solution. I added logic to put empty td back and forth in *

 <?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>"; ?> 
+4
source

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

+1
source

I used the code below.

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

0
source
 <?php for($i=0;$i<=5;$i++){ for($j=5;$j>=$i;$j--){ echo '&nbsp;'; } for($k=0;$k<=$i;$k++){ echo '*'; } echo '<br />'; } for($i=0;$i<=4;$i++){ for($k=0;$k<=$i+1;$k++){ echo '&nbsp;'; } for($j=4;$j>=$i;$j--){ echo '*'; } echo '<br />'; } ?> 
-1
source

Source: https://habr.com/ru/post/1260352/


All Articles