php While循环只在FPDF中打印1个结果

qltillow  于 2022-10-30  发布在  PHP
关注(0)|答案(1)|浏览(195)

我想调用基于“where $bulan”的数据,在数据库$bulan中有一些相同的数据,但如果我调用只显示第一个输入。我已经使用while,但它不工作
你们能帮忙吗?
我有这样的代码

<?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();
}
ctehm74n

ctehm74n1#

(A)请将下列行移出while循环:
1.$pdf =新的FPDF(“l”,“mm”,“A4”);
1.输出();
(B)请同时添加$pdf-〉Open();在上述(1)之后
[NOTE:此点(B)是not所需的new versions,例如1.8x或更高...]
new FPDF用于创建类FDPF的新对象,对于同一对象示例不应重复和重复;输出行应该在创建整个PDF文档后触发(因此在while循环之外)
因此,请更改(为了清楚起见,我从块中删除了一些行):

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();
}

至(样品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();

(样品2)
如果你想在一页中显示所有的数据,那么移动行$pdf -〉AddPage();并将其放在new语句下
沙盒:
http://www.createchhk.com/SOanswers/sub8/testSO26Oct2022c.php
编码:

$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();

另一方面,正如其他人建议的那样,请将您的查询改为参数化预准备语句,以避免SQL注入。详细信息请参考以下内容:
https://www.php.net/manual/en/mysqli.quickstart.prepared-statements.php

Sanbox链接

您可以尝试以下沙箱来查看示例的效果(db中有3条记录,显示两个字段):
http://www.createchhk.com/SOanswers/sub7/testSO26Oct2022.php
(For较新版本,例如FPDF 1.8x):http://www.createchhk.com/SOanswers/sub8/testSO26Oct2022b.php

相关问题