Dealing with multiple lines of code within a variable

2

I'm developing a dynamic report with mPDF, but I'm not able to put my data inside a variable so I can 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'm using AJAX to load the core of my index.

    
asked by anonymous 05.07.2016 / 20:59

1 answer

2

First of all, you are mixing variable with playing data 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 here:

$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 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 that you think is best.

See more about HereDoc in this post:

  

What is used

05.07.2016 / 21:04