0
Good evening guys, I come with one more question in a loop in php.
I need to create a PDF of a very extensive report. An alternative that the MPDF itself indicates, is to create blocks and go writing the pages one by one. It will be an overload but, I need to generate a pdf, with a report that can have up to 100 thousand records (I intend to display 100 per page or go adapting), so the MPDF can not receive all html at once.
The point is that I have an array, where each index receives a row of data (a tag <tr>
) which I receive from the database. But I wanted to group this into blocks of 100 for example, ie each index, group 100 lines.
My current array follows this format
$tr = [
0 => "<p>Valor 1</p>",
1 => "<p>Valor 2</p>",
2 => "<p>Valor 3</p>",
3 => "<p>Valor 4</p>",
4 => "<p>Valor 5</p>",
...
];
In the example above, if it were for example 2 elements per index, I would need it to look like this:
$tr = [
0 => "<p>Valor 1</p> <p>Valor 2</p>",
1 => "<p>Valor 3</p> <p>Valor 4</p>",
2 => "<p>Valor 5</p>" ...
];
I tried to make a loop, which looks more like a gambiarra, but gives some index errors and ends up losing some elements, what I tried was this:
$group = [];
for($i = 0; $i <= count($tr); $i++) {
for($i2 = 0; $i2 < 100; $i2++) {
$group[$i] .= $tr[$i2];
unset($tr[$i2]);
}
$tr = array_values($tr);
}
What I tried to do is, with each loop loop loop, fill up to 100, and the loop of the second is I make an unset by cleaning that variable, so when I exit the down and go to the next outside loop, rearrange the array without the items that have already been accounted for.
Someone can help me with this logic. I’m not able to think of a clean way and form it