2017-01-13 8 views
0

Ich brauche eine Webseite, die eine PDF-Datei erzeugt, die mehrere Werte für dieselbe Zeile in einer sql db -Tabelle zurückgibt. Im Moment funktioniert es, da es die Ergebnisse liefert, die ich brauche, aber nur für die Tabellenreihe Nummer 1. Aber ich habe 1600+ Zeilen und ich hätte gerne die PDF mit allen Zeilen. (A4 pdf, mit 2 Ergebnissen pro Seite)FOR LOOP - brauchbare Unterstützung

-Code

<?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

Ihre Db-Schnittstelle sieht so aus: s zieht nur eine einzige Zeile: '$ db-> get_row'. Jeder kann Ihnen sagen, wie man eine Schleife implementiert, aber ohne Ihre DB-Klasse zu sehen, haben wir keine Möglichkeit zu wissen, wie Sie alle Datensätze aus der DB statt einer einzigen Zeile ziehen können. –

+0

meine db-klasse? Entschuldigung, aber .. was meinst du ?! –

+0

Ich meine die Datei: 'ez_sql.inc', die Ihre Datenbank-Interaktionen behandelt. Ich habe auf Github nachgeschaut, aber im Moment ist Github offline. –

Antwort

1

basierend auf der doc hier: https://ezsql.github.io/ezSQL/ez_sql_help.htm das Verfahren $db->get_row() retreives nur eine Zeile aus der Datenbank. alle Zeilen zu erhalten, müssen Sie $db->get_results() statt

Dann haben Sie die Ergebnisse in einer Schleife, verwenden, um eine foreach-Schleife, wie am Beispiel von der doc gezeigt:

Gelöst
$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, die tatsächlich irgendwie funktioniert .. Ich habe getan, wie Sie verdaut, und jetzt ist mein Ergebnis die Daten auf allen Feldern der letzten Zeile .. aus irgendeinem Grund wiederholt es nicht den Code. –

+0

@RuiNeves ok das ist ein Fortschritt, können Sie den aktualisierten Code in Ihrer Frage hinzufügen? –

+0

C̅R̅O̅Z̅E̅T̅ Ich kann den ganzen Code hier nicht einfügen, weil es für einen Kommentar zu lang ist, aber hier ist, was ich geändert habe: $ sozio = $ db-> get_results ("SELECT * FROM socios"); foreach ($ sozio als $ sozio1) {und ich rufe jetzt die DB info mit $ sozio1 –

0

!!!

ging mit CROZET suggestion und fügte eine $ count var hinzu, um die HTML-Wiederholung zu erleichtern.

danke.