We have answer of your question!

100% solved queries, no empty question

Question: creating triangle pattern table in php


0

Advertisement


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?

Question author Eka-putra | Source

Answer


1


Advertisement


Perhaps you could add another array for the empty columns something like this.

$table = '<table border ="1" cellpadding="10px" cellspacing="10px" style="border-collapse: collapse">';
$rows = array('5', '3', '1', '3', '5');
//created an object array for how may empty columns should be added on both before and after the not empty columns base on the $rows array.
$emptyColObj = array(5=>0,3=>1,1=>2);
foreach($rows as $row){
    $rowArray = $row;
    echo $emptyRow;
    $table .= "<tr>";
    //create a loop to make an empty column
    //before not empty column
    for($e=0;$e<$emptyColObj[$rowArray];$e++){
        $table .= "<td></td>";
    }
    $cols = array('1', '2', '3', '4', '5');
    for ($j=0; $j<$rowArray; $j++){
        $array[$j] = $cols[$j];
        $table .= "<td>". $array[$j] ."</td>";
    }
    //create a loop to make an empty column
    //after not empty column
    for($e=0;$e<$emptyColObj[$rowArray];$e++){
        $table .= "<td></td>";
    }
    $table .= "</tr>";  
}
$table .= '</table>';
echo $table;

Output:

---------------------
| 1 | 2 | 3 | 4 | 5 |
|--------------------
|   | 2 | 3 | 4 |   |
|--------------------
|   |   | 3 |   |   |
|--------------------
|   | 2 | 3 | 4 |   |
|--------------------
| 1 | 2 | 3 | 4 | 5 |
|--------------------
Answer author Mark-vincent-manjac

Advertisement


Tickanswer.com is providing the only single recommended solution of the question creating triangle pattern table in php under the categories i.e php , . Our team of experts filter the best solution for you.

Related Search Queries:

pascal triangle in php code , number pattern programs in php , number pyramid program in php , numeric pyramid in php , pattern in php example , str_repeat in php , prime number program in php ,

You may also add your answer!