使用FPDF PHP在PDF表上显示mysql数据库

amrnrhlw  于 2023-03-28  发布在  PHP
关注(0)|答案(1)|浏览(78)

我的目标是使用FPDF在PDF上显示表格形式的数据(没有使用 “单元格”)。在PDF创建中使用 “echo” 创建表格的问题是不可接受的。
有什么想法吗?

ubbxdtey

ubbxdtey1#

// retrieve student data
            $student = new Student();
            $id = 1;
            $stmt = $conn->prepare("SELECT name, email FROM student WHERE ID = ?");
            $stmt->bind_param("i", $id);
            $stmt->execute();
            $stmt->bind_result($student->name, $student->email);
            $stmt->fetch();
            $stmt->close();

            // retrieve grades
            $stmt = prepare("SELECT course_title, letter_grade FROM databas WHERE student_id = ?");
            $stmt->bind_param("i", $id);

            $stmt->bind_result($course_title, $letter_grade);
            while ($stmt->fetch()) {
                $student->grades[] = array("course_title" => $course_title, "letter_grade" => $letter_grade);
            }
            $stmt->close();

            $stmt = $conn->prepare("SELECT course_title, letter_grade FROM javascript WHERE student_id = ?");
            $stmt->bind_param("i", $id);
            $stmt->execute();
            $stmt->bind_result($course_title, $letter_grade);
            while ($stmt->fetch()) {
                $student->grades[] = array("course_title" => $course_title, "letter_grade" => $letter_grade);
            }
            $stmt->close();

            // generate PDF
            $pdf = new PDF();
            $pdf->AliasNbPages();
            $pdf->AddPage();

            $pdf->SetFont('Arial','B',12);
            $pdf->Cell(0,10,'Name of the student : '.$student->name,0, 0, 'L');
            $pdf->Cell(0,10,'Date : '.date('d-m-Y'),0, 0, 'R');

            $pdf->Ln(5);
            $pdf->Cell(35,10,'Student ID : '.$id,0, 0);
            $pdf->Ln();

            $pdf->SetFont('Arial','',10);
            foreach ($student->grades as $grade) {
                $pdf->Cell(70,10,$grade["course_title"],0, 0);
                $pdf->Cell(70,10,$grade["letter_grade"],0, 0);
                $pdf->Ln();
            }

            $pdf->Ln(10);
            $pdf->Cell(0, 10, 'Signature: ________________________', 0, 1, 'R');

            $pdf->Output();
            ?>

相关问题