根据 mysql (PHP) 中的选定列制作 pdf 报告

make pdf report based on selected columns from mysql (PHP)

我一直在寻找更简单的方法来生成基于 mysql 的 selected 列的 fpdf 报告。示例:

   _________________________________________________________
   |NO |NAME    |   COMPANY   |    ADDRESS    |   PHONE    |
   |-------------------------------------------------------|
   | 1 | Andrew |    AT&T     |   123 Street  |    123456  |
   | 2 | Darwin |   Verizon   |   888 Road    |    222222  |
   |___|________|_____________|_______________|____________|

我制作了包含复选框的表单,因此用户可以 select he/she 想要生成哪些列,例如 :

[] 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 多列 table,如果我仍然使用嵌套 if.

提前致谢。

不太确定你在问什么,但可能是这样的:

for(loop){
    if (isset($_POST['Name'])) {
       $pdf->Cell(Name);
    }
    if (isset($_POST['Company'])) {
       $pdf->Cell(Company);
    }
    if (isset($_POST['Address'])) {
        $pdf->Cell(Address);
    }
    etc etc
    $pdf->Ln();
}