table html printing

Asked

Viewed 1,888 times

1

I am developing a php application with mysql and at a certain point need to make a query in the database and bring the information in a table. Until then quiet, however I need to have the option to print all the contents of the table, ie print (on paper) a dynamic query.

I studied ireport but I’m having a hard time, and I’d like to know if there’s another way to make that impression or not. In another part of the application I print certificates and it is very quiet, but the ireport gives me the impression that I will have to do some tricks to use with php.

Here is an example of the table I’m using, and remembering that sometimes the search generates several pages.

 <table cellspacing="0" cellpadding="0" border="0" class="display" id="listausers">
    <thead>
    <tr>
        <th>N°Registro</th><th>Beneficiário</th><th>Data Emissão</th><th>CPF</th><th>CNH</th><th>Data do Cadastro</th></th><th>Operações</th>
    </tr>
    </thead>
    <tbody> 
    <?php
    $data_atual=explode("/" , date('d/m/Y'));
    $ano_atual=$data_atual[2];
    $alvara_vaga_idoso = new alvara_vaga_idoso();
    $alvara_vaga_idoso->selecionaTudo($alvara_vaga_idoso);
    while($res = $alvara_vaga_idoso->retornaDados()):
        echo '<tr>';
        printf('<td>%s</td>',$res->num_registro ."/".$ano_atual);
        printf('<td>%s</td>',$res->nome_beneficiario);
        printf('<td>%s</td>',$res->data_emissao);
        printf('<td>%s</td>',$res->cpf);
        printf('<td>%s</td>',$res->cnh);
        printf('<td class="center">%s</td>',date("d/m/Y - H:i:s",strtotime($res->datacad)));

        printf('<td class="center"><a href="?m=alvara_vaga_idoso&t=incluir_alvaras_vaga_idoso" title="Novo Cadastro"> <img src="images/add.png" alt="Novo Cadastro"/> </a> <a href="?m=alvara_vaga_idoso&t=editar_alvara_vaga_idoso&num_registro=%s" title="Editar Cadastro"> <img src="images/edit.png" alt="Editar Cadastro"/> </a></a> <a href="?m=alvara_vaga_idoso&t=visualizar_alvara_vaga_idoso&num_registro=%s" title="Visualizar Cadastro"> <img src="images/view.png" alt="Visualizar Cadastro"/><a href="?m=alvara_vaga_idoso&t=imprimir_alvara_vaga_idoso&num_registro=%s" title="Imprimir Credencial"> <img src="images/printer.png" alt="Imprimir Credencial"/> </a> </td>',$res->num_registro,$res->num_registro,$res->num_registro);
        echo '</tr>';
    endwhile;
    ?>
    </tbody>
</table>
  • You generate this table on screen, and it will have a print button, would that be?

  • That’s right Rodrigo, my difficulty is like printing when it’s more than one page

  • vc generates a table with paging? would be?

1 answer

2


In the head of your page put:

<link rel="stylesheet" href="print.css" media="print" />

Create the file print.css and style the elements of your table the way you need and give display:none in the elements you don’t want to appear on the print page.

Then create a print button on your php page:

echo '<a href="#" onClick="print();">Imprimir</a>';

If you prefer to put a separate printing page, you can do the same process on it by simply replacing the above php code with:

echo '<script>print();</script>';

And on the previous page print button you send the user to this new page. They are two functional ways.

  • Dalton thanks for the answer, my question is whether this way will print all pages of the table, for example: depending on the survey generates 3 pages and depending generates 10 pages

  • 2

    @mayconrocha I used this solution in a system and it worked for all pages to be printed. What I recommend is that you do a test/simulation of the solution.

  • Thank you very much Dalton, I’ll test it here

  • 1

    @mayconrocha, you made it?

  • Dalton, I’m sorry for the lack of experience

  • 1

    @mayconrocha, what went wrong in the tests?

  • 1
Show 2 more comments

Browser other questions tagged

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