Printing only the last result

Asked

Viewed 40 times

0

I’m putting together a PDF by MPDF, using a HTML. But he has printando only the last result and should printar all, for it is within one foreach. How to make it to print all the result one in each row.

<?php
foreach ($results as $k => $v) {
        $html = '
    <html>
    <head>
    </head>
    <body>
          <table class="flat-table">
          <tr>
            <th>Nosso Numero</th>
            <th>Ocorrencia</th>
            <th>Vlr Boleto</th>
            <th>Matricula</th>
            <th>Nome</th>
            <th>Competencia</th>
            <th>Valor Devido</th>
            </tr>
            <tr>
            <td>'.$v->nosso_numero.'</td>
            <td>'.$v->ocorrencia.'</td>
            <td>'.$v->vlr_boleto.'</td>
            <td>'.$v->matricula.'</td>
            <td>'.$v->nome.'</td>
            <td>'.$v->competencia.'</td>
            <td>'.$v->valor_devido.'</td>
            </tr>
          </table>
    </body>
    </html>
    ';
    }
    $this->mpdf->WriteHTML($html);
    $this->mpdf->Output();
  • For each foreach element you override the value of $html so it just feels like the last.

3 answers

2

Boas, you must concatenate the $html variable in each iteration:

$html = ' <html>
    <head>
    </head>
    <body>
          <table class="flat-table">
          <tr>
            <th>Nosso Numero</th>
            <th>Ocorrencia</th>
            <th>Vlr Boleto</th>
            <th>Matricula</th>
            <th>Nome</th>
            <th>Competencia</th>
            <th>Valor Devido</th>
          </tr>';

foreach ($results as $k => $v) {
        $html .= '
            <tr>
            <td>'.$v->nosso_numero.'</td>
            <td>'.$v->ocorrencia.'</td>
            <td>'.$v->vlr_boleto.'</td>
            <td>'.$v->matricula.'</td>
            <td>'.$v->nome.'</td>
            <td>'.$v->competencia.'</td>
            <td>'.$v->valor_devido.'</td>
            </tr>
    ';
    }

$html .= '</table>
    </body>
    </html>';

2


First of all your loop is wrong, because it is placing the whole page structure inside the repetition, so you would generate several "pages" in a single.

Then, as done by Marty, one should only repeat the records:

# Gera as tags principais, e o cabeçalho da table
$html = ' <html>
    <head>
    </head>
    <body>
          <table class="flat-table">
          <tr>
            <th>Nosso Numero</th>
            <th>Ocorrencia</th>
            <th>Vlr Boleto</th>
            <th>Matricula</th>
            <th>Nome</th>
            <th>Competencia</th>
            <th>Valor Devido</th>
          </tr>';

# Gera linhas da tabela
foreach ($results as $k => $v) {
        $html .= '
            <tr>
             <td>'.$v->nosso_numero.'</td>
             <td>'.$v->ocorrencia.'</td>
             <td>'.$v->vlr_boleto.'</td>
             <td>'.$v->matricula.'</td>
             <td>'.$v->nome.'</td>
             <td>'.$v->competencia.'</td>
             <td>'.$v->valor_devido.'</td>
            </tr>
    ';
    }

# Fecha a table e as outras tags principais
$html .= '</table>
    </body>
    </html>';

Complementing

You must use .=, to add the data to your variable $html, because if you don’t have the . which means concatenation, you will replace all values, staying only with the last, which in the case of the above example, would be only </table>.

0

There was one detail missing from $html, now she concatenates $html and does not subscribe. with the use of the += will concatenate instead of subscribing.

<?php
$html = "";
foreach ($results as $k => $v) {
        $html+= '
    <html>
    <head>
    </head>
    <body>
          <table class="flat-table">
          <tr>
            <th>Nosso Numero</th>
            <th>Ocorrencia</th>
            <th>Vlr Boleto</th>
            <th>Matricula</th>
            <th>Nome</th>
            <th>Competencia</th>
            <th>Valor Devido</th>
            </tr>
            <tr>
            <td>'.$v->nosso_numero.'</td>
            <td>'.$v->ocorrencia.'</td>
            <td>'.$v->vlr_boleto.'</td>
            <td>'.$v->matricula.'</td>
            <td>'.$v->nome.'</td>
            <td>'.$v->competencia.'</td>
            <td>'.$v->valor_devido.'</td>
            </tr>
          </table>
    </body>
    </html>
    ';
    }
    $this->mpdf->WriteHTML($html);
    $this->mpdf->Output();
  • With this occurs: Message: Undefined variable: html

  • 1

    I’m sorry, I made the correction already

  • 2

    @Bulfaitelo this way will create several "pages" in use same page. It is correct that!?

  • @Rbz will depend on how is the autopagebreak of mptf it. usually is I leave auto.

  • As a guy, he will print 1 sheet for expensive record !? Then there is no why the table...

  • The + sign is sum, not concatenation in PHP.

  • @Rbz actually didn’t set it, but the important thing is that he can print

  • @Sam in the case when using += by default php concatenates.

  • "but the important thing is that it can print" or do it right !? So, you are "correcting the error".

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.