FPDF 中的 While 循环仅首先检索数据 time.It 完美打印 pdf 中的 table 但不输入第一次检索到的数据
While loop in FPDF retrieve data only first time.It prints the table in pdf perfectly but doesn't input the data retrieved after first time
我正在打印一个 table,其中填充了使用 FPDF 库从数据库中检索到的数据。我希望此代码 运行 直到结束并打印 table.It 中分组的学生数据正在检索和打印第一组的数据,但对于其他组,它仅打印 table 并且不使用数据填充它。
这是我的代码。
session_start();
require ('../make_db_connection.php');
require ("../fpdf/fpdf.php");
$pdf=new FPDF('P','mm','A4');
$res = $conn->query("SELECT * FROM student_data");
$num=$res->num_rows;
while ($num > 0)
{
$pdf->AddPage();
if ($num != 0)
{
$row = $res->fetch_assoc();
$title=$row['title'];
$date=$row['sub_date'];
$supervisor=$row['supervisor'];
$res = $conn->query("SELECT name,roll_no,abstract FROM student_data WHERE title='".$title."' AND supervisor='".$supervisor."'");
}
$pdf->SetFillColor(232,232,232);
$pdf->SetMargins(30,0,20);
$pdf->SetFont('Times','B',14);
$pdf->Cell(0,10,'',0,1);
$pdf->Cell(0,7,"FYP Evaluation Sheet",0,1,'C');
$pdf->SetFont('Times','B',13);
$pdf->Cell(0,4,'',0,1);
$pdf->Cell(40,7,"GR#:",0,0,'L');
$pdf->Cell(30,7,"Project Title:",0,0,'L');
$pdf->SetFont('Times','',11);
$pdf->MultiCell(90,7,$title,'','L',FALSE);
//Table
$pdf->SetFont('Times','B',13);
$pdf->Cell(0,4,'',0,1);
$pdf->Cell(40,7,"Individuals:",0,0,'L');
$pdf->Cell(0,10,'',0,1);
$x=$pdf->GetX();
$y=$pdf->GetY();
$pdf->MultiCell(35,10, 'Reg#','LRTB','C',TRUE);
$pdf->SetXY($x+35,$y);
$pdf->MultiCell(70,10, 'Name','LTB','C', TRUE);
$pdf->SetXY($x+105,$y);
$pdf->MultiCell(50,10, 'Marks & Comments','LRTB','C', TRUE);
$pdf->MultiCell(0,0, '','R',FALSE);
$pdf->SetFont('Times','',10);
$x=$pdf->GetX();
$y=$pdf->GetY();
$row = $res->fetch_assoc();
$student1=$row['name'];
$student1_rollno=$row['roll_no'];
$num=$num-1;
$pdf->MultiCell(35,8,$student1_rollno,'LRB','C',0);
$pdf->SetXY($x+35,$y);
$pdf->MultiCell(70,8,$student1,'B','C', 0);
$pdf->SetXY($x+105,$y);
$pdf->MultiCell(50,8,'','LRB','L', 0);
$x=$pdf->GetX();
$y=$pdf->GetY();
$row = $res->fetch_assoc();
if($row)
{
$student2=$row['name'];
$student2_rollno=$row['roll_no'];
$pdf->MultiCell(35,8,$student2_rollno,'LRB','C',0);
$pdf->SetXY($x+35,$y);
$pdf->MultiCell(70,8,$student2,'B','C', 0);
$pdf->SetXY($x+105,$y);
$pdf->MultiCell(50,8,'','LRB','L', 0);
$num=$num-1;
}
$pdf->MultiCell(100,8, 'Note: Please fill the form and submit with name and signature.','','L', 0);
}
ob_end_clean();
$pdf->output();
我在这段代码中做错了什么?我想不通!
因为我对 OP 的评论得到了解决,所以我会把我的答案放在这里。
您使用 $res
变量在 while
循环中存储查询结果,这会覆盖之前存储的结果。
我正在打印一个 table,其中填充了使用 FPDF 库从数据库中检索到的数据。我希望此代码 运行 直到结束并打印 table.It 中分组的学生数据正在检索和打印第一组的数据,但对于其他组,它仅打印 table 并且不使用数据填充它。 这是我的代码。
session_start();
require ('../make_db_connection.php');
require ("../fpdf/fpdf.php");
$pdf=new FPDF('P','mm','A4');
$res = $conn->query("SELECT * FROM student_data");
$num=$res->num_rows;
while ($num > 0)
{
$pdf->AddPage();
if ($num != 0)
{
$row = $res->fetch_assoc();
$title=$row['title'];
$date=$row['sub_date'];
$supervisor=$row['supervisor'];
$res = $conn->query("SELECT name,roll_no,abstract FROM student_data WHERE title='".$title."' AND supervisor='".$supervisor."'");
}
$pdf->SetFillColor(232,232,232);
$pdf->SetMargins(30,0,20);
$pdf->SetFont('Times','B',14);
$pdf->Cell(0,10,'',0,1);
$pdf->Cell(0,7,"FYP Evaluation Sheet",0,1,'C');
$pdf->SetFont('Times','B',13);
$pdf->Cell(0,4,'',0,1);
$pdf->Cell(40,7,"GR#:",0,0,'L');
$pdf->Cell(30,7,"Project Title:",0,0,'L');
$pdf->SetFont('Times','',11);
$pdf->MultiCell(90,7,$title,'','L',FALSE);
//Table
$pdf->SetFont('Times','B',13);
$pdf->Cell(0,4,'',0,1);
$pdf->Cell(40,7,"Individuals:",0,0,'L');
$pdf->Cell(0,10,'',0,1);
$x=$pdf->GetX();
$y=$pdf->GetY();
$pdf->MultiCell(35,10, 'Reg#','LRTB','C',TRUE);
$pdf->SetXY($x+35,$y);
$pdf->MultiCell(70,10, 'Name','LTB','C', TRUE);
$pdf->SetXY($x+105,$y);
$pdf->MultiCell(50,10, 'Marks & Comments','LRTB','C', TRUE);
$pdf->MultiCell(0,0, '','R',FALSE);
$pdf->SetFont('Times','',10);
$x=$pdf->GetX();
$y=$pdf->GetY();
$row = $res->fetch_assoc();
$student1=$row['name'];
$student1_rollno=$row['roll_no'];
$num=$num-1;
$pdf->MultiCell(35,8,$student1_rollno,'LRB','C',0);
$pdf->SetXY($x+35,$y);
$pdf->MultiCell(70,8,$student1,'B','C', 0);
$pdf->SetXY($x+105,$y);
$pdf->MultiCell(50,8,'','LRB','L', 0);
$x=$pdf->GetX();
$y=$pdf->GetY();
$row = $res->fetch_assoc();
if($row)
{
$student2=$row['name'];
$student2_rollno=$row['roll_no'];
$pdf->MultiCell(35,8,$student2_rollno,'LRB','C',0);
$pdf->SetXY($x+35,$y);
$pdf->MultiCell(70,8,$student2,'B','C', 0);
$pdf->SetXY($x+105,$y);
$pdf->MultiCell(50,8,'','LRB','L', 0);
$num=$num-1;
}
$pdf->MultiCell(100,8, 'Note: Please fill the form and submit with name and signature.','','L', 0);
}
ob_end_clean();
$pdf->output();
我在这段代码中做错了什么?我想不通!
因为我对 OP 的评论得到了解决,所以我会把我的答案放在这里。
您使用 $res
变量在 while
循环中存储查询结果,这会覆盖之前存储的结果。