对于循环 - 需要一些可靠的帮助

时间:2017-01-13 16:10:50

标签: php html mysql pdf

我需要一个生成PDF文件的网页,该文件为sql db表中的同一行返回多个值。现在它正在工作,因为它得到我需要的结果,但仅适用于表行第1行。但我有1600多行,我想要所有行的pdf ..(A4 pdf,每页2个结果)

代码

<?php

require_once('include/menu.php');
include_once("mfpdf/mpdf.php");
include_once('include/ez_sql.inc');

setlocale(LC_MONETARY, 'pt_PT');
$socio = $db->get_results("SELECT * FROM socios where estado = 'ACTIVO'");

$mpdf=new mPDF('c','A4','','',32,25,27,25,16,13);
$mpdf->SetDisplayMode('fullpage');
$mpdf->list_indent_first_level = 0;

$mpdf -> allow_charset_conversion=true;
$mpdf->charset_in='utf-8';

// $stylesheet = file_get_contents('mfpdf/mpdfstyletables.css');
$mpdf->WriteHTML($stylesheet,1);    // *** The parameter 1 tells that this    is css/style only and no body/html/text
$cont = 0;

foreach ($socio as $socio1) {

$cont = $cont + 1;
$ano = date("Y");
$idade = age(dtPadrao($socio->nascimento));
if ($idade >= 18) $Vquota = 1.50;
else $Vquota = 1;

$Vquota = money_format('%.2n', $Vquota);

$html = '
<br /><br />
<table width="794px" height="462px" align="center" style="border: 0px solid #880000; background-color: #FFF; " class="tallcells">
    <tr>
        <td height="462px" width="250px" valign="top" align="left">
            <br><br><img src="img/sfal_logo.png" height="140px"><br><br><br>
            <table border="2px" align="center">
            <tbody>
                <tr>
                    <td align="left"><font size="5"><h3>SOCIO N&ordm; '.$socio1->n_socio.'</h3><br />
                            '.$socio1->nome.'<br />
                            '.$socio1->morada.'<br />
                            '.$socio1->cod_postal.' - '.$socio1->localidade.'<br />
                            <br />Ano '.$ano.'<br />
                            Valor Mensal: '.$Vquota.'
                            <br /><br />
                            Local de Cobran&ccedil;a:<br />
                            '.$socio1->local_cobranca.'</font><br />
                    </td>
                </tr>
            </tbody>
            </table>
        </td>
        <td width="544px">                      <!-- was 10px  10px-->
            <table cellpadding="0px" cellspacing="15px" align="center">
                <tbody>
                <tr>
                    <td style="border: 1px solid #880000; background-color: #FFF;" width="190px" class="tallcells" align="center"><font size="5">
                        SFAL
                        <br />S&oacute;cio n.: '.$socio1->n_socio.'
                        <br />Valor:  '.$Vquota.'
                        <br />DEZEMBRO
                        <br />'.$ano.'</font></td>
                    <td style="border: 1px solid #880000; background-color: #FFF; " width="190px" class="tallcells" align="center"><font size="5">
                        SFAL
                        <br />S&oacute;cio n.: '.$socio1->n_socio.'
                        <br />Valor:  '.$Vquota.'
                        <br />NOVEMBRO
                        <br />'.$ano.'</font></td>
                    <td style="border: 1px solid #880000; background-color: #FFF; " width="190px" class="tallcells" align="center"><font size="5">
                        SFAL
                        <br />S&oacute;cio n.: '.$socio1->n_socio.'
                        <br />Valor:  '.$Vquota.'
                        <br />OUTUBRO
                        <br />'.$ano.'</font></td>
                </tr>
                <tr>
                    <td style="border: 1px solid #880000; background-color: #FFF; " class="tallcells" align="center"><font size="5">
                        SFAL
                        <br />S&oacute;cio n.: '.$socio1->n_socio.'
                        <br />Valor:  '.$Vquota.'
                        <br />SETEMBRO
                        <br />'.$ano.'</font></td>
                    <td style="border: 1px solid #880000; background-color: #FFF; " class="tallcells" align="center"><font size="5">
                        SFAL
                        <br />S&oacute;cio n.: '.$socio1->n_socio.'
                        <br />Valor:  '.$Vquota.'
                        <br />AGOSTO
                        <br />'.$ano.'</font></td>
                    <td style="border: 1px solid #880000; background-color: #FFF; " class="tallcells" align="center"><font size="5">
                        SFAL
                        <br />S&oacute;cio n.: '.$socio1->n_socio.'
                        <br />Valor:  '.$Vquota.'
                        <br />JULHO
                        <br />'.$ano.'</font></td>
                </tr>
                <tr>
                    <td style="border: 1px solid #880000; background-color: #FFF; " class="tallcells" align="center"><font size="5">
                        SFAL
                        <br />S&oacute;cio n.: '.$socio1->n_socio.'
                        <br />Valor:  '.$Vquota.'
                        <br />JUNHO
                        <br />'.$ano.'</font></td>
                    <td style="border: 1px solid #880000; background-color: #FFF; " class="tallcells" align="center"><font size="5">
                        SFAL
                        <br />S&oacute;cio n.: '.$socio1->n_socio.'
                        <br />Valor:  '.$Vquota.'
                        <br />MAIO
                        <br />'.$ano.'</font></td>
                    <td style="border: 1px solid #880000; background-color: #FFF; " class="tallcells" align="center"><font size="5">
                        SFAL
                        <br />S&oacute;cio n.: '.$socio1->n_socio.'
                        <br />Valor:  '.$Vquota.'
                        <br />ABRIL
                        <br />'.$ano.'</font></td>
                </tr>
                <tr>
                    <td style="border: 1px solid #880000; background-color: #FFF; " class="tallcells" align="center"><font size="5">
                        SFAL
                        <br />S&oacute;cio n.: '.$socio1->n_socio.'
                        <br />Valor:  '.$Vquota.'
                        <br />MAR&Ccedil;O
                        <br />'.$ano.'</font></td>
                    <td style="border: 1px solid #880000; background-color: #FFF; " class="tallcells" align="center"><font size="5">
                        SFAL
                        <br />S&oacute;cio n.: '.$socio1->n_socio.'
                        <br />Valor:  '.$Vquota.'
                        <br />FEVEREIRO
                        <br />'.$ano.'</font></td>
                    <td style="border: 1px solid #880000; background-color: #FFF; " class="tallcells" align="center"><font size="5">
                        SFAL
                        <br />S&oacute;cio n.: '.$socio1->n_socio.'
                        <br />Valor:  '.$Vquota.'
                        <br />JANEIRO
                        <br />'.$ano.'</font></td>
                </tr>
            </tbody>
            </table>
        </td>
    </tr>
</table>
<br /><br /><br />
';

//==============================================================
//==============================================================
//==============================================================



// $html = mb_convert_encoding($html, 'UTF-8', 'UTF-8');


$mpdf->WriteHTML($html,2);
if ($cont == 50) break;

//==============================================================
//==============================================================
//==============================================================

}
$mpdf->Output('mpdf.pdf','I');
exit;
?>

2 个答案:

答案 0 :(得分:1)

基于此处找到的文档:https://ezsql.github.io/ezSQL/ez_sql_help.htm 方法$db->get_row()仅从数据库中检索一行。要获取所有行,您需要使用$db->get_results()代替

然后,你必须使用foreach循环循环结果,如doc中的示例所示:

$users = $db->get_results("SELECT name, email FROM users");

foreach ( $users as $user )
{
            // Access data using object syntax
            echo $user->name;
            echo $user->email;
}

答案 1 :(得分:0)

解决!!!

使用了CROZET的消息并添加了$ count var以简化html的重复。

谢谢。