i have problem for creating triangle pattern table in php, and i write code like this,
echo "<table border ='1' cellpadding='10px' cellspacing='10px' style='border-collapse: collapse'>";
$rows = array('5', '3', '1', '3', '5');
foreach($rows as $row){
$rowArray = $row;
echo "<tr>";
$cols = array('1', '2', '3', '4', '5');
for ($j=0; $j<$rowArray; $j++){
$array[$j] = $cols[$j];
echo "<td>". $array[$j] ."</td>";
}
echo "</tr>";
}
echo "</table><br>";
the result
---------------------
| 1 | 2 | 3 | 4 | 5 |
|--------------------
| 1 | 2 | 3 | | |
|--------------------
| 1 | | | | |
|--------------------
| 1 | 2 | 3 | | |
|--------------------
| 1 | 2 | 3 | 4 | 5 |
|--------------------
but I was expecting for the results as below
---------------------
| 1 | 2 | 3 | 4 | 5 |
|--------------------
| | 2 | 3 | 4 | |
|--------------------
| | | 3 | | |
|--------------------
| | 2 | 3 | 4 | |
|--------------------
| 1 | 2 | 3 | 4 | 5 |
|--------------------
anyone can explain, step by step to create the results as I expected?
<td>
- you need to pad with the appropriate number of empty<td>
elements before it. The same is true of the rows with the value of 3.