【问题标题】:make pdf report based on selected columns from mysql (PHP)根据 mysql (PHP) 中的选定列制作 pdf 报告
【发布时间】: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。

提前致谢。

【问题讨论】:

    标签: php mysql pdf report fpdf


    【解决方案1】:

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

    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();
    }
    

    【讨论】:

    • 抱歉不清楚,我要问的是是否有更有效的方法来只生成特定的列,所以pdf页面会有更多的空间。如果我显示所有列,即使在横向中,空间也不够用,因为某些列的记录长度超过 100 个字符。如果我使用我的旧代码,总共会有 >4000 行,这完全不方便。顺便说一句,我也可以用你的方法来 //Header 和 //Array 吗?
    • 但这就是我所做的。如果在 POST 中选中一列,它将显示它。对标题做同样的事情。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多