Handling multiple lines of code within a variable

Asked

Viewed 749 times

2

I am developing a dynamic report with mPDF, but I am not able to put my data inside a variable to be able to output it.

My page is designed like this:

$html = '<div class="box-content no-padding table-responsive relMembros">'
.'<table class="table table-bordered table-striped table-hover table-heading table-datatable" id="tabMembros">'
.'    <thead>'
.'        <tr>'
.'            <th>Nome</th>'
.'            <th>Telefone</th>'
.'            <th>Aniversario</th>'
.'            <th>Email</th>'
.'            <th>Úsuario mobile</th>'
.'            <th>Status</th>'
.'        </tr>'
.'    </thead>'
.'    <tbody>';
        $conexao = new ConexaoDatabase();

        $sql = "MINHA QUERY";

        $sqlVars = array();
        $sqlVars[':igj'] = $suc->getCOD_IDENT_IGREJ();

        $registros = $conexao->fetchAll($sql, $sqlVars);

        if ($registros) {

            foreach ($registros as $registro) {

                echo '<tr>'
                . '<td>' . $registro->TXT_NOMEX_PESSO . '</td>'
                . '<td>' . $registro->TXT_FONEX_PESSO . '</td>'
                . '<td>' . $registro->DAT_NASCI_PESSO . '</td>'
                . '<td>' . $registro->TXT_EMAIL_PESSO . '</td>'
                . '<td>' . $registro->FLG_USUAR_MOBIL . '</td>'
                . '<td>' . $registro->FLG_STATU_PESSO . '</td>'
                . '</tr>';
            }
        } else {
            //echo 'Não existe vinculo para está pessoa.';
        }                     
    $html = $html . '</tbody>'
.'</table>'
.'</div>';

include("../pdf/mpdf60/mpdf.php");

$mpdf=new mPDF(); 

$mpdf->SetDisplayMode('fullpage');

$mpdf->WriteHTML($html);

$mpdf->list_number_suffix = ')';

$mpdf->WriteHTML($html);

$mpdf->Output();
exit;
?>

To open this page I am using AJAX to load the core of my index.

1 answer

2


First of all, you are mixing variable with playing dice on the screen, see the difference:

if ($registros) {

            foreach ($registros as $registro) {

                $html = $html . '<tr>'
                . '<td>' . $registro->TXT_NOMEX_PESSO . '</td>'
                . '<td>' . $registro->TXT_FONEX_PESSO . '</td>'
                . '<td>' . $registro->DAT_NASCI_PESSO . '</td>'
                . '<td>' . $registro->TXT_EMAIL_PESSO . '</td>'
                . '<td>' . $registro->FLG_USUAR_MOBIL . '</td>'
                . '<td>' . $registro->FLG_STATU_PESSO . '</td>'
                . '</tr>';
            }
        } else {
            //echo 'Não existe vinculo para está pessoa.';
        }                     

In addition, this can be simplified:

$html = $html . '<tr>'

is the same as

$html .= '<tr>'

What allows this:

$html .= '<tr>'
  . '<tr>'
  . '<td>' . $registro->TXT_NOMEX_PESSO . '</td>'
  . '<td>' . $registro->DAT_NASCI_PESSO . '</td>'
  ...


Using the HEREDOC:

PHP already has a suitable method for large blocks of text, see it applied to your code:

$html = <<<CODIGO
<div class="box-content no-padding table-responsive relMembros">
<table class="table table-bordered table-striped table-hover table-heading table-datatable" id="tabMembros">
    <thead>
        <tr>
            <th>Nome</th>
            <th>Telefone</th>
            <th>Aniversario</th>
            <th>Email</th>
            <th>Úsuario mobile</th>
            <th>Status</th>
        </tr>
    </thead>
    <tbody>
CODIGO;

    $conexao = new ConexaoDatabase();

    $sql = "MINHA QUERY";
    $sqlVars = array();
    $sqlVars[':igj'] = $suc->getCOD_IDENT_IGREJ();

    $registros = $conexao->fetchAll($sql, $sqlVars);

    if ($registros) {
        foreach ($registros as $registro) {
            $html .= <<<CODIGO
              <tr>
                <td>{$registro->TXT_NOMEX_PESSO}</td>
                <td>{$registro->TXT_FONEX_PESSO}</td>
                <td>{$registro->DAT_NASCI_PESSO}</td>
                <td>{$registro->TXT_EMAIL_PESSO}</td>
                <td>{$registro->FLG_USUAR_MOBIL}</td>
                <td>{$registro->FLG_STATU_PESSO}</td>
             </tr>
CODIGO;
        }
    } else {
        $html .= 'Não existe vinculo para está pessoa.';
    }                     
    $html .= <<<CODIGO
         </tbody>
       </table>
    </div>
CODIGO;

    include("../pdf/mpdf60/mpdf.php");
    $mpdf=new mPDF(); 
    $mpdf->SetDisplayMode('fullpage');
    $mpdf->WriteHTML($html);
    $mpdf->list_number_suffix = ')';
    $mpdf->WriteHTML($html);
    $mpdf->Output();

Instead of the CODE delimiter you can use the string you think best.

See more about Heredoc in this post:

What use <<< EOH in PHP?


Other ways

It goes of taste and context, but see another way to deal with several lines:

$html .= '<div class="box-content no-padding table-responsive relMembros">';
$html .= '  <table class="table table-bordered table-striped table-hover table-heading table-datatable" id="tabMembros">';
$html .= '    <thead>';
$html .= '      <tr>';
$html .= '        <th>Nome</th>';
$html .= '        <th>Telefone</th>';
$html .= '        <th>Aniversario</th>';
$html .= '        <th>Email</th>';
$html .= '        <th>Úsuario mobile</th>';
$html .= '        <th>Status</th>';
$html .= '      </tr>';
$html .= '    </thead>';
$html .= '  <tbody>';

 ...

     foreach ($registros as $registro) {
        $html .= '<tr>';
        $html .= '  <td>' . $registro->TXT_NOMEX_PESSO . '</td>';
        $html .= '  <td>' . $registro->TXT_FONEX_PESSO . '</td>';
        $html .= '  <td>' . $registro->DAT_NASCI_PESSO . '</td>';
        $html .= '  <td>' . $registro->TXT_EMAIL_PESSO . '</td>';
        $html .= '  <td>' . $registro->FLG_USUAR_MOBIL . '</td>';
        $html .= '  <td>' . $registro->FLG_STATU_PESSO . '</td>';
        $html .= '</tr>';
    }


Formatting to look nice in HTML

If you want, you can still avoid whitespace and leave HTML formatted using \t and \n;

$html .= "\t\t<tr>\n";
$html .= "\t\t\t<th>Nome</th>\n";
$html .= "\t\t\t<th>Telefone</th>\n";

Whereas the \t will become tabulations, and the \n line breaks.

  • Bag of mPDF also ?

  • I never used it. I use the FPDF without the HTML part (In the FPDF you draw directly in the page positions).

  • Because he is opening the PDF on the same page, or is not interpreting the command but putting all that code on the screen

  • Ai ta missing hit the headers.

  • you know how it would be ?

  • header("Content-type:application/pdf"); and if you want to force download header("Content-Disposition:attachment;filename='downloaded.pdf'"); - usually the first one is enough. Remember that this is right at the beginning of the page, before any data is sent. You can’t even have space before the first <?php not to interfere.

  • it’s not worked

  • see that you’re calling $mpdf->Writehtml($html); twice, I think it’s kind of weird that too.

  • You say that "is opening on the same page"... There is more on the page besides the PDF? Can not mix. Either you serve the page to open in the browser, or you make the PDF. It has to be PHP separated for two things (or have a logic to show only one thing or only the other).

  • Yes, because I do next, I have the index, inside the index I have a div chamaod content, when I click the button to generate the pdf it redirects me via ajax to the page where I put the content inside the index.

  • So, you have to separate this logic. Either show on the screen, or generate the pdf. If you want you can even put a link in the part that shows on the screen, but this link has to call PHP again to generate PDF.

  • Let’s just see how to do this kkkk

  • To simplify, you can make an if( $_GET['PDF'] == 1 ) shows on the screen, and shows the link contents.php? PDF=1 ELSE sends the headers, and makes the output. So ajax will call without PDF=1, then it will output HTML. But if one clicks on the link, you have ? PDF=1 that changes the logic and only generates the PDF. Thus, the generation is the same, but the IF selects whether to HTML or PDF pro client.

  • I will try to implement here

  • Good fun kkk. But if you screw up badly, remember that you can ask a separate question with the specific problem (but do not forget to put the relevant parts of the code). If you are going to "train", you can make a PDF version, and a single screen, to test, then you put it together in one. Or you can use includes. geraconteudo.php makes the HTML, then the ajax.php makes include of geraconteudo.php and plays on the screen, and the pdf.php also makes include the geraconteudo.php, but makes a PDF. So you separate everything and reuse.

  • Get here, I’m opening a popup with the pdf

  • I will see a way to treat the popup so it always open on the screen and inside a box I will see what I do. Thank you very much helped me

Show 12 more comments

Browser other questions tagged

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