I have an array of items. There are two "types" or items: a heading row, and a regular row. I want to print the heading only if there are any "regular" rows under it (before the next heading).
(This is a simplification - in practice I wont be able to look at the next item from in the current iteration).
To solve this, I used buffer control. I clear and start the buffer at each heading row, and in each regular row I flush the buffer. So the heading wont print if there is a heading after it (buffer will be cleaned) and will be flushed to user if there is a regular row to "flush" it out.
(psudeo code)
for i in array:
if i is heading:
ob_end_clean();
ob_start();
echo "header $i";
else:
ob_end_flush();
echo "regular $i"
This works great.
Problem is, I have an option to download the page as a PDF and this broke it. The way it works is if the pdf flag is on, at the top of the page, buffer was started and at the bottom of the code, the buffer was dumped into a variable and sent to the pdf api as a string. Now, the buffer is sent to the user at ob_end_flush() which produces this error when I try to download the pdf:
cannot modify header information - headers already sent.
I like my solution but is there a way to make it work? Or is it a bad solution to begin with? thanks