I am working on an attendance sheet module. What I am suppose to do is that write a simple select query which will select worker name and staff id from the table and output it using fpdf in a sheet in portrait orientation.
My code is:
<?php
$date = 'Month: '.date("M-Y");
$sr=1;
//$empname = '$_GET["selectuser"]';
$f=0.75*10;
require('fpdf.php');
/*------------------------- header columns ---------------------------------*/
$conn = new mysqli('localhost', 'tahir', 'abcdef', '12345');
if($conn->connect_error){
die("Error in DB connection: ".$conn->connect_errno." : ".$conn->connect_error);
}
$select = "SELECT * FROM tbl_th";
$result = $conn->query($select);
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',14);
while($row = $result->fetch_object()){
$empname = $row->emp_name;
$memberid = $row->member_id;
$trade = $row->trade;
// $pdf=new FPDF();
// $pdf->AddPage(); $pdf->SetFont('Arial','B',$f);
$pdf->SetX(5);
$pdf->SetY(15);
$pdf->MultiCell(194,10,"Attendance Sheet",1,'C', false);
$pdf->SetX(5);
$pdf->SetY(25);
$pdf->Cell(194,10,$date,1,1,'C',false);
$pdf->SetX(5);
$pdf->SetY(35);
$pdf->Cell(15,10,"Sr.No.",1,'C', true);
$pdf->SetX(5);
$pdf->SetY(35);
$pdf->MultiCell(90,10,"Worker Name",1,'C', false);
$pdf->SetX(5);
$pdf->SetY(35);
$pdf->MultiCell(115,10,"Staff ID",1,'R', false);
$pdf->SetX(5);
$pdf->SetY(35);
$pdf->MultiCell(145,10,"Trade",1,'R', false);
$pdf->SetX(5);
$pdf->SetY(35);
$pdf->MultiCell(194,10,"Signature",1,'R', false);
/*------------------------- columns start ---------------------------------*/
$pdf->SetX(5);
$pdf->SetY(45);
$pdf->Cell(15,10,$sr++,1,'C', true);
$pdf->SetX(5);
$pdf->SetY(45);
$pdf->MultiCell(90,10,$empname,1,'C', false);
$pdf->SetX(5);
$pdf->SetY(45);
$pdf->MultiCell(115,10,$memberid,1,'R', false);
$pdf->SetX(5);
$pdf->SetY(45);
$pdf->MultiCell(145,10,$trade,1,'R', false);
$pdf->SetX(5);
$pdf->SetY(45);
$pdf->MultiCell(194,10,"",1,'R', false);
$pdf->Output();
}
?>
I was expecting it to display all the records of the table. But it is only displaying the first record of the table.
I can’t get it to scroll through all the records. I’m done. I can’t. I wanna keep using fpdf. I don’t wanna switch to other options.
Please help me what is that I am missing in this script of mine? Please thanks in advance!
>Solution :
It looks like the problem is that you are calling the $pdf->Output() function inside the while loop. This function sends the PDF output to the browser and ends the script, so the loop only runs once and only the first record is displayed. To fix this, you should move the $pdf->Output() function outside of the while loop, after all the records have been added to the PDF. Additionally, you should also add $pdf->AddPage() inside the while loop to create a new page for each record, otherwise all the records will be added to the same page.