I need to create pdf document which outputs all rows from mysql table, but in code attached bellow it only outputs one last row from that table. Does someone have time to check my code and suggest me correction or does someone have a better code for such report?
3 Answers
Answer on my question is correctly answered in question answered under the following link: TCPDF - printing table from mysql showing repeated first row
Code is here:
1 Comment
Tiago
Code missing here.
$db = mysql_connect($hostname, $db_user, $db_password);
mysql_select_db($database,$db);
$rnBr = mysql_real_escape_string(trim($_POST['rnBr']));
$zahtjev = $_GET['rnBr'];
$upit = "SELECT * FROM radni where formDatum >= CURDATE() and formDatum < (CURDATE()+ interval 7 day) order by formDatum asc";
$result = mysql_query($upit);
require_once('../config/lang/hrv.php');
require_once('../tcpdf.php');
// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('XXXXX');
$pdf->SetTitle('TCPDF Radni nalog');
$pdf->SetSubject('TCPDF Radni nalog');
$pdf->SetKeywords('TCPDF, PDF, radni, nalog, ispis');
//set some language-dependent strings
$pdf->setLanguageArray($l);
// ---------------------------------------------------------
// set font
$pdf->SetFont('dejavusans', '', 14, '', true);
// add a page
$pdf->AddPage();
$tbl = <<<EOD
<table border="" cellpadding="0" cellspacing="7" align="center" fontsize="14">
<tr>
<td></td><td><br /><br /><br /><br /></td>
</tr>
<tr>
<th colspan="2"><h1>Tjedni raspored djelatnika</h1></th>
</tr>
</table>
EOD;
$pdf->writeHTML($tbl, true, false, false, false, '');
$pdf->Ln();
$pdf->SetLineStyle(array('width' => 0.0, 'cap' => 'butt', 'join' => 'miter', 'dash' => 4, 'color' => array(255, 0, 0)));
$pdf->SetFillColor(255,255,128);
$pdf->SetTextColor(0,0,128);
$pdf->Ln();
$tbl = <<<EOD
<table border="1" cellpadding="0" cellspacing="3" align="center" fontsize="12">
<tr>
<th colspan="1">Radni nalog:</th>
<th colspan="1">Datum:</th>
<th colspan="1">Izvršitelj:</th>
<th colspan="1">Tvrtka:</th>
<th colspan="1">Poslovnica:</th>
<th colspan="1">Napomena:</th>
<th colspan="1">RN izdao:</th>
</tr>
</table>
EOD;
$pdf->writeHTML($tbl, true, false, false, false, '');
$pdf->Ln();
$tbl = '<table border="1" cellpadding="0" cellspacing="3" align="center" fontsize="10">';
while($row = mysql_fetch_assoc($result)) {
$tbl .="<tr>
<td>{$row['rnBr']}</td>
<td>{$row['formDatum']}</td>
<td>{$row['formIzvrsio']}</td>
<td>{$row['formTvrtka']}</td>
<td>{$row['formPoslovnica']}</td>
<td>{$row['formNapomene']}</td>
<td>{$row['formIzdavatelj']}</td>
</tr>";
}
$tbl = '</table>';
$pdf->writeHTML($tbl, true, false, false, false, '');
// Set some content to print
$html = <<<EOD
<i>Ovaj popis generiran je računalnim programom!</i>
EOD;
$pdf->writeHTMLCell($w='', $h='', $x='', $y='', $html, $border=1, $ln=1, $fill=1, $reseth=true, $align='center', $autopadding=true);
// ---------------------------------------------------------
//Close and output PDF document
$pdf->Output('raspored_djelatnika.pdf', 'I');
Comments
One way to do it:
the while loop
(
if($number_of_rows > 0) {
while($row = mysql_fetch_assoc($result))
{
$RN = $row['rnBr'];
$Tvrtka = $row['formTvrtka'];
$Poslovnica = $row['formPoslovnica'];
$Datum = $row['formDatum'];
$Izvrsio = $row['formIzvrsio'];
$Kontakt = $row['formKontakt'];
$Napomene = $row['formNapomene'];
$Izdavatelj = $row['formIzdavatelj'];
}
}
)
Needs to be down where this is:
<tr>
<td>$RN</td>
<td>$Datum</td>
<td>$Izvrsio</td>
<td>$Tvrtka</td>
<td>$Poslovnica</td>
<td>$Napomene</td>
<td>$Izdavatelj</td>
</tr>
So you'll get something like
if($number_of_rows > 0) {
while($row = mysql_fetch_assoc($result))
{
echo '<tr>';
echo '<td>'. $row['rnBr'].'</td>';
echo '<td>'. $row['formTvrtka'].'</td>';
echo '<td>'. $row['formPoslovnica'].'</td>';
echo '<td>'. $row['formDatum'].'</td>';
echo '<td>'. $row['formIzvrsio'].'</td>';
echo '<td>'. $row['formKontakt'].'</td>';
echo '<td>'. $row['formNapomene'].'</td>';
echo '<td>'. $row['formIzdavatelj'].'</td>';
}
}
otherwise yes, you only output the last row that the query found.
Alternatively change each line like:
$RN = $row['rnBr']; to $RN[$n] = $row['rnBr']; (with $n++ at the end of your loop and $n=0; before the loop
Then at the bottom when outputting you can do:
for($u=0;$u<count($rn);$u++){
<tr>
<td>$RN[$u]</td>
<td>$Datum[$u]</td>
<td>$Izvrsio[$u]</td>
<td>$Tvrtka[$u]</td>
<td>$Poslovnica[$u]</td>
<td>$Napomene[$u]</td>
<td>$Izdavatelj[$u]</td>
</tr>
}
1 Comment
mdakic
i have used both first and second solution and always gives me the same last one raw from table