问题描述
我找不到使用FPDF制作表格并从MySQL数据库中获取数据的体面教程.我只想知道如何创建一个.在网络上尝试样品我一直在遇到很多错误.
例如,我有列,名字,中间名,姓氏,年龄和电子邮件.
如何使用fpdf创建表格并从数据库中呼应条目?
推荐答案
阅读并遵循 教程 在FPDF网站上可能是一个不错的开始.
假设您有一个表(我们称其为people)和样本的数据
CREATE TABLE People (id int, first_name varchar(5), middle_name varchar(4), last_name varchar(5), age int, email varchar(15)); INSERT INTO People (id, first_name, middle_name, last_name, age, email) VALUES (1, 'Jhon', NULL, 'Doe', 27, 'jhon@email.com'), (2, 'Mark', 'J', 'Lee', 35, 'mark@email.com'), (3, 'Helen', 'P', 'Smith', 30, 'helen@email.com');
这是一个基本的PHP脚本,可以按照您的意愿进行操作. 注意:代码缺乏为Brevity的缘故处理任何错误.
<?php require('fpdf.php'); class People { public function all() { try { $db = new PDO('mysql:host=localhost;dbname=test;charset=UTF-8', 'user', 'password'); $query = $db->prepare("SELECT first_name, middle_name, last_name, age, email FROM people "); $query->execute(); $people = $query->fetchAll(PDO::FETCH_ASSOC); } catch (PDOException $e) { //echo "Exeption: " .$e->getMessage(); $result = false; } $query = null; $db = null; return $people; } } class PeoplePDF extends FPDF { // Create basic table public function CreateTable($header, $data) { // Header $this->SetFillColor(0); $this->SetTextColor(255); $this->SetFont('','B'); foreach ($header as $col) { //Cell(float w [, float h [, string txt [, mixed border [, int ln [, string align [, boolean fill [, mixed link]]]]]]]) $this->Cell($col[1], 10, $col[0], 1, 0, 'L', true); } $this->Ln(); // Data $this->SetFillColor(255); $this->SetTextColor(0); $this->SetFont(''); foreach ($data as $row) { $i = 0; foreach ($row as $field) { $this->Cell($header[$i][1], 6, $field, 1, 0, 'L', true); $i++; } $this->Ln(); } } } // Column headings $header = array( array('First Name', 30), array('Middle Name', 30), array('Last Name', 30), array('Age', 12), array('Email', 47) ); // Get data $people = new People(); $data = $people->all(); $pdf = new PeoplePDF(); $pdf->SetFont('Arial', '', 12); $pdf->AddPage(); $pdf->CreateTable($header,$data); $pdf->Output();
确保更改连接字符串
$db = new PDO('mysql:host=localhost;dbname=test;charset=UTF-8', 'user', 'password'); ^^^^^^^^^ ^^^^ ^^^^ ^^^^^^^^
其他推荐答案
楼上的答案有益于您的问题的主要原因,但我建议您采用 tcpdf 而不是fpdf,因为第一个比第二个要快,并且如果要创建一个非常大的文件(考虑您从数据库中发现的行数) tcpdf 表现非常好. 还要考虑使用 zend_pdf 首先影响的模块很难更难使用其他人,但目前是最快的库.我的观点是tha,如果您在我提到的情况下,可以创建一个使用zend_pdf和 zend_db ,请轻松完成所有工作.
问题描述
I can't find a decent tutorials for making tables using fpdf and fetching data from the mysql database. I just want to know how to create one. I've been getting lot of errors for trying samples across the web.
For example, I have columns, First Name, Middle Name, Last Name, Age, and Email.
How do I create a table using fpdf and echo the entries from the database?
推荐答案
Reading and following a tutorial on FPDF site might be a good start.
Assuming that you have a table (lets call it people) and sample data like this
CREATE TABLE People (id int, first_name varchar(5), middle_name varchar(4), last_name varchar(5), age int, email varchar(15)); INSERT INTO People (id, first_name, middle_name, last_name, age, email) VALUES (1, 'Jhon', NULL, 'Doe', 27, 'jhon@email.com'), (2, 'Mark', 'J', 'Lee', 35, 'mark@email.com'), (3, 'Helen', 'P', 'Smith', 30, 'helen@email.com');
Here is a basic php script that do what you want. Note: code lacks any error handling for brevity's sake.
<?php require('fpdf.php'); class People { public function all() { try { $db = new PDO('mysql:host=localhost;dbname=test;charset=UTF-8', 'user', 'password'); $query = $db->prepare("SELECT first_name, middle_name, last_name, age, email FROM people "); $query->execute(); $people = $query->fetchAll(PDO::FETCH_ASSOC); } catch (PDOException $e) { //echo "Exeption: " .$e->getMessage(); $result = false; } $query = null; $db = null; return $people; } } class PeoplePDF extends FPDF { // Create basic table public function CreateTable($header, $data) { // Header $this->SetFillColor(0); $this->SetTextColor(255); $this->SetFont('','B'); foreach ($header as $col) { //Cell(float w [, float h [, string txt [, mixed border [, int ln [, string align [, boolean fill [, mixed link]]]]]]]) $this->Cell($col[1], 10, $col[0], 1, 0, 'L', true); } $this->Ln(); // Data $this->SetFillColor(255); $this->SetTextColor(0); $this->SetFont(''); foreach ($data as $row) { $i = 0; foreach ($row as $field) { $this->Cell($header[$i][1], 6, $field, 1, 0, 'L', true); $i++; } $this->Ln(); } } } // Column headings $header = array( array('First Name', 30), array('Middle Name', 30), array('Last Name', 30), array('Age', 12), array('Email', 47) ); // Get data $people = new People(); $data = $people->all(); $pdf = new PeoplePDF(); $pdf->SetFont('Arial', '', 12); $pdf->AddPage(); $pdf->CreateTable($header,$data); $pdf->Output();
Make sure to change connection string
$db = new PDO('mysql:host=localhost;dbname=test;charset=UTF-8', 'user', 'password'); ^^^^^^^^^ ^^^^ ^^^^ ^^^^^^^^
其他推荐答案
The upstairs answers are good for the main reason of your problem, but I suggest you to adopt TCPDF instead of fpdf, because the first one is faster than the second, and if you are going to create a very large file (considering the number of rows that you'll find out from you database) TCPDF as very good performance. Consider also to use Zend_PDF module that on first impact is harder to use that the others, but is the fastest library at the moment. My opinio is tha , if you are in the case I've mentioned, you can create a bigger class (in OOP programming) that use zend_pdf and Zend_DB and easly do all the work.