1

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?

4
  • 1
    The row with a value of 1, you only have one <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.
    – mseifert
    Jan 7, 2017 at 2:36
  • Thanks for your concern, but this is still confusing, from way I do its right?
    – Eka putra
    Jan 7, 2017 at 2:59
  • 1
    Forget php for the moment. How would you make the table in html?
    – Teepeemm
    Jan 7, 2017 at 3:55
  • oh,..i see, I began to understand, so I had to make the first empty column many thanks
    – Eka putra
    Jan 7, 2017 at 4:16

1 Answer 1

1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.