【发布时间】:2016-08-31 23:31:05
【问题描述】:
我一直在寻找更简单的方法来根据 mysql 中的选定列生成 fpdf 报告。示例:
_________________________________________________________
|NO |NAME | COMPANY | ADDRESS | PHONE |
|-------------------------------------------------------|
| 1 | Andrew | AT&T | 123 Street | 123456 |
| 2 | Darwin | Verizon | 888 Road | 222222 |
|___|________|_____________|_______________|____________|
我制作了包含复选框的表单,以便用户可以选择他/她想要生成的列:
[] Name
[] Company
[] Address
[] Phone
如果我想显示姓名和公司,现在这是我的初始代码:
if(isset($_POST['Name'])){
if(isset($_POST['Company'])){
(I copied the code below and pasted here, then I added:
$this->Cell(6,1,'Company','TB',0,'L',1); in //HEADER
$cell[$i][2]=$d[2]; in //ARRAY
$pdf->Cell(6,1,$cell[$j][1],'B',0,'L'); in //PDF
)
}
class PDF extends FPDF{
//HEADER
function Header(){
$this->SetTextColor(128,0,0);
$this->SetFont('Arial','B','8');
$this->SetFont('Arial','B','7');
$this->SetFillColor(192,192,192);
$this->SetTextColor(0,0,0);
$this->Cell(1,1,'No','TB',0,'L',1);
$this->Cell(5,1,'Name','TB',0,'L',1);
$this->Ln();
}
}
$net = new mysqli($server, $user, $pass, $data);
if($net->connect_error){
die("Connection: ".$net->connect_error);
}
$q = "select * from people where name between 'Andrew' and 'Darwin'";
$h = $net->query($q) or die($net->error);
$i = 0;
//ARRAY
while($d=$h->fetch_array()){
$cell[$i][0]=$d[0];
$cell[$i][1]=$d[1];
$i++;
}
//PDF
$pdf = new PDF('L','cm','A4');
$pdf->Open();
$pdf->AliasNbPages();
$pdf->AddPage();
$pdf->SetFont('Arial','','6');
for($j=0;$j<$i;$j++){
$pdf->Cell(1,1,$j+1,'B',0,'L');
$pdf->Cell(5,1,$cell[$j][0],'B',0,'L');
$pdf->Ln();
}
$pdf->Output();
}
输出如下:
____________________________
|NO |NAME | COMPANY |
|--------------------------|
| 1 | Andrew | AT&T |
| 2 | Darwin | Verizon |
|___|________|_____________|
如您所见,我正在使用嵌套 if 方法,我想知道是否有任何有效且更简单的方法可以做到这一点,因为我的真实表中有 10 多列,如果我仍然使用嵌套的 if。
提前致谢。
【问题讨论】: