Search code examples
phpfpdf

While loop only print 1 result in FPDF


i want to call data based on "where $bulan", in database $bulan have some of the same data but if i call only show the first input. I already use while but it's not working

can you guys help?

I have code like this

<?php
require_once("fpdf/fpdf.php");
require_once('koneksi.php');

$no = 1;
$bulan = $_POST['bulan'];

$data = mysqli_query($conn, "SELECT proses1.*, customer.nickname FROM proses1 inner join
customer on customer.id_cust = proses1.id_cust WHERE proses1.tgl = '$bulan'");
while ($row = mysqli_fetch_array($data)) {
$pdf = new FPDF('l','mm','A4');
// membuat halaman baru
$pdf -> AddPage();
$pdf -> SetFont('Times','B','10');
// judul
$pdf->Cell(15,10,'',0,0,'L');
$pdf->Cell(100,20,'PT Ganding Toolsindo',0,2,'L');

$pdf -> SetFont('Times','','12');
$pdf->Cell(260,10,'Rekap Proses 1',0,2,'C');
$pdf -> image('dist/img/gandingrbg.png',10,13,15,15 );

$pdf -> SetFont('Times','','10');
$pdf->Cell(230,10,'Periode Bulan :',0,0,'R');
$pdf->Cell(20,10,date('d-M-Y', strtotime($_POST['bulan'])),0,1,'R');

$pdf -> SetFont('Times','B','10');
$pdf->Cell(10,10,'No',1,0,'C');
$pdf->Cell(17,10,'CUST',1,0,'C');
$pdf->Cell(50,10,'Nama Part',1,0,'C');
$pdf->Cell(17,10,'Kode Part',1,0,'C');
$pdf->Cell(30,10,'Nama Proses',1,0,'C');
$pdf->Cell(40,10,'Quantity Masuk',1,0,'C');
$pdf->Cell(45,10,'Quantity Not Good',1,0,'C');
$pdf->Cell(30,10,'Keterangan',1,0,'C');
$pdf->Cell(40,10,'Tanggal Dikerjakan',1,1,'C');

$pdf -> SetFont('Times','','10');
$pdf->Cell(10,10,$no++,1,0,'C');
$pdf->Cell(17,10,$row['nickname'],1,0,'C');
$pdf->Cell(50,10,$row['nama_part'],1,0,'C');
$pdf->Cell(17,10,$row['kode_part'],1,0,'C');
$pdf->Cell(30,10,$row['nama_proses'],1,0,'C');
$pdf->Cell(40,10,$row['qty_aktual'],1,0,'C');
$pdf->Cell(45,10,$row['qty_ng'],1,0,'C');
$pdf->Cell(30,10,$row['keterangan'],1,0,'C');
$pdf->Cell(40,10,$row['tgl'],1,1,'C');



$pdf->Output();
}

Solution

  • (A) Please move the following lines outside of your while loop:

    1. $pdf = new FPDF('l','mm','A4');
    2. $pdf->Output();

    (B) Please also add $pdf->Open(); after (1) above

    [NOTE : this point (B) is not needed for new versions such as 1.8x or later...]

    new FPDF is for creating a new object of the class FDPF, which should not be repeated and repeated for the same object instance; the output line should be triggered after the whole PDF document is created (so outside the while loop)

    So change (I have removed some lines from the block for clarity):

    while ($row = mysqli_fetch_array($data)) {
    $pdf = new FPDF('l','mm','A4');
    // membuat halaman baru
    $pdf -> AddPage();
    $pdf -> SetFont('Times','B','10');
    // judul
    $pdf->Cell(15,10,'',0,0,'L');
    $pdf->Cell(100,20,'PT Ganding Toolsindo',0,2,'L');
    
    // other lines for output omitted for clarity
    
    $pdf->Cell(30,10,$row['keterangan'],1,0,'C');
    $pdf->Cell(40,10,$row['tgl'],1,1,'C');
    
    $pdf->Output();
    }
    

    to (sample 1)

    
    $pdf = new FPDF('l','mm','A4');
    
    $pdf->Open();
    // the above line is not needed for newer versions of FPDF (e.g. v1.8x)
    
    while ($row = mysqli_fetch_array($data)) {
    
    // membuat halaman baru
    $pdf -> AddPage();
    $pdf -> SetFont('Times','B','10');
    // judul
    $pdf->Cell(15,10,'',0,0,'L');
    $pdf->Cell(100,20,'PT Ganding Toolsindo',0,2,'L');
    
    // other lines for output omitted for clarity
    
    $pdf->Cell(30,10,$row['keterangan'],1,0,'C');
    $pdf->Cell(40,10,$row['tgl'],1,1,'C');
    
    }
    
    $pdf->Output();
    
    

    (sample 2)

    If you want to display all the data in 1 page, then move the line $pdf -> AddPage(); away from the loop and place it under the new statement

    Sandbox:

    http://www.createchhk.com/SOanswers/sub8/testSO26Oct2022c.php

    Code:

    
    $pdf = new FPDF('l','mm','A4');
    $pdf -> AddPage();
    
    $pdf->Open();
    // the above line is not needed for newer versions of FPDF (e.g. v1.8x)
    
    while ($row = mysqli_fetch_array($data)) {
    
    // membuat halaman baru
    
    $pdf -> SetFont('Times','B','10');
    // judul
    $pdf->Cell(15,10,'',0,0,'L');
    $pdf->Cell(100,20,'PT Ganding Toolsindo',0,2,'L');
    
    // other lines for output omitted for clarity
    
    $pdf->Cell(30,10,$row['keterangan'],1,0,'C');
    $pdf->Cell(40,10,$row['tgl'],1,1,'C');
    
    }
    
    $pdf->Output();
    
    

    On the other hand, as other has suggested please change your query to parameterized prepared statement to avoid SQL injection. You may refer to the following for details:

    https://www.php.net/manual/en/mysqli.quickstart.prepared-statements.php

    Sanbox link

    You may try the following sandbox to see the effect of a sample (3 records in db, showing two fields) :

    http://www.createchhk.com/SOanswers/sub7/testSO26Oct2022.php

    (For newer version , e.g. FPDF 1.8x): http://www.createchhk.com/SOanswers/sub8/testSO26Oct2022b.php