2017-01-13 26 views
0

我需要一個網頁生成一個PDF文件,該文件爲SQL數據庫表中的同一行返回多個值。現在它正在工作,因爲它正在得到我需要的結果,但僅適用於表格行號1.但是,我有1600多行,並且我希望所有行都有pdf ..(A4 pdf,每頁2個結果)FOR LOOP - 需要一些有價值的協助

代碼

<?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; 
?> 
+0

你的DB接口看起來像它;它只拉動一行:'$ db-> get_row'。任何人都可以告訴你如何實現一個循環,但沒有看到你的數據庫類,我們無法知道如何從數據庫中取出所有記錄,而不是一行。 –

+0

我的db類?對不起,但是..你是什麼意思? –

+0

我指的是處理數據庫交互的文件:'ez_sql.inc'。我在github上查了它,但是現在github處於脫機狀態。 –

回答

1

基礎上,DOC這裏找到:https://ezsql.github.io/ezSQL/ez_sql_help.htm 方法$db->get_row() retreives從數據庫中只有一行。讓所有的行,你需要使用$db->get_results()代替

然後,你必須使用如圖所示的例子從文檔foreach循環的結果循環:

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

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

CROZET這實際上是一種工作..我做你的擁塞,現在我的結果是在最後一行所有領域的數據..由於某種原因,它不重複的代碼。 –

+0

@RuiNeves好的,這是一個進步,你可以在你的問題中添加更新的代碼? –

+0

C̅R̅O̅Z̅E̅T̅我不能把所有的代碼放在這裏,因爲它太長的評論,但這裏是我改變了: $ socio = $ db-> get_results(「SELECT * FROM socios」); foreach($ socio as $ socio1){我現在用$ socio1調用數據庫信息 –

0

解決!

與CROZET的消化一起,並添加了$ count var來緩解html重複。

謝謝。