0

I've to print a table using TCPDF for creating a PDF using PHP from an array of contents.

In my example I'll use 7 entries, but can be more or less.

$array = array(
 0 => "[ ] Option 1",
 1 => "[x] Option 2",
 2 => "[ ] Option 3",
 3 => "[ ] Option 4",
 4 => "[x] Option 5",
 5 => "[ ] Option 6",
 6 => "[ ] Option 7",
 ...
);

My table layout is dynamic: I can set numbers of columns for display purpose ( $num_cols can be 1, 2, 3 or 4 columns)

Based on my $num_cols, I've to split my content of the array in each TD cell.

So, if I set $num_cols = 4, i need to show something like that:

<table>
  <tr>
    <td>[ ] Option 1</td>
    <td>[x] Option 2</td>
    <td>[ ] Option 3</td>
    <td>[ ] Option 4</td>
  </tr>
  <tr>
    <td>[x] Option 5</td>
    <td>[ ] Option 6</td>
    <td>[ ] Option 7</td>
    <td></td>
  </tr>
</table>

I tried to start some ideas but in this example I can get only a row. I don't know how to create the external loop:

echo "<table>";
echo "<tr>";

for ( $i = 0; $i < $num_cols; $i++ ) {

 echo "<td>";
 if ( isset($array[$i]) {echo $array[$i];} else {}
 echo "</td>";

}
echo "</tr>";
echo "</table>";
2
  • Side note: you do not need empty else statements. You can get rid of your 'else {}'. Commented Oct 30, 2018 at 16:58
  • Looks like you want to investigate nested loops. Commented Oct 30, 2018 at 17:02

2 Answers 2

4

array-chunk will help you:

<?php
$num_cols = 4;
$array = [1,2,3,4,5,6,7,8];

echo "<table>";
foreach (array_chunk($array, $num_cols) as $chunk) {
    echo "<tr>";
    foreach ($chunk as $item) {
        echo "<td>";
        echo $item;
        echo "</td>";
    }
    echo "</tr>";
}
echo "</table>";

and you can use some other cool-stuff from php :)

<?php
$num_cols = 4;
$array = [1,2,3,4,5,6,7,8];

echo "<table><tr><td>" .
    implode("</td></tr><tr><td>",
        array_map(function(array $part) {
            return implode('</td><td>', $part);
        }, array_chunk($array, $num_cols))
    )
. "</td></tr></table>";
Sign up to request clarification or add additional context in comments.

Comments

2
<?php
$array = array(
 0 => "[ ] Option 1",
 1 => "[x] Option 2",
 2 => "[ ] Option 3",
 3 => "[ ] Option 4",
 4 => "[x] Option 5",
 5 => "[ ] Option 6",
 6 => "[ ] Option 7"
);
$num_cols = 4;

//Use $i as a counter
$i = 0;
echo '<table>';
foreach ($array as $key => $val) {
    //If we are outputting the first column in a row, create a new tr tag
    if ($i % $num_cols == 0)
        echo '<tr>';

    //Output the column
    echo '<td>' . $val . '</td>';

    //If we are at the end of the number of columns, close the tr tag
    if ($i % $num_cols == 3)
        echo '</tr>';
    $i++;
}

//If we didn't close the tr tag in the loop, close it now
if ($i % $num_cols != 0)
    echo '</tr>';
echo '</table>';
?>

7 Comments

Way too heavy for an easy problem. You should use existing php functions which are here to help you out, rather than trying to recreate what someone else already implemented. See the other answer with array_chunk.
Yeah I could have use array_chunk but I came up with the proper solution without it. Just because I don't use a native function doesn't mean I should be downvoted...
This is not about not using native functions. Readability is at stake here. Your code is way harder to understand when the other solution is tacit in the glimpse of an eye.
i put a plus for you @Mic1780 ^_^ peace!
I don't see anything wrong with this answer. It modifies the OP's code and yields the expected results that being sought. The comments are never bad (as long as they make sense). While I think both answers are good.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.