【问题标题】:FPDF, generating pdf from mysql table for selected valueFPDF,从 mysql 表中为选定值生成 pdf
【发布时间】:2013-12-02 01:36:58
【问题描述】:

你好朋友下面是我的 php 代码,它从 MYSQL 表(所有行)生成 PDF 并且像任何东西一样工作,我需要为选定的行生成报告,只说例如存储在的第 1 行和第 3 行一个数组($sel),我不知道如何使用 for、foreach 和 while 循环来实现这一点......任何帮助将不胜感激。

这是我的数据库表

 ----------------------------------
| id | company            | total  |
 ----------------------------------
| 1  | ABC                | 1000   |
| 2  | XYZ                |  500   |
| 3  | PZX                |  100   |
 ----------------------------------

这是我的 php 页面,它在加载时生成 pdf 并提供该文件的下载链接

<?php

require('u/fpdf.php');//fpdf path
include_once('db_connect.php');// my db connection

$sel = array (1,3);

$result=mysql_query("select * from `receipt`  ");

//Initialize the 3 columns and the total
$c_code = "";
$c_name = "";
$c_price = "";
$total = 0;

//For each row, add the field to the corresponding column
while($row = mysql_fetch_array($result))
{ 
   $code =$row['id'];
   $name = substr($row['company'],0,20);
   $real_price = $row['total'];
   $show =$row['total'];

 $c_code = $c_code.$code."\n";
 $c_name = $c_name.$name."\n";
 $c_price = $c_price.$show."\n";

//Sum all the Prices (TOTAL)
    $total = $total+$real_price;
}
mysql_close();

$total = $total;

//Create a new PDF file
$pdf=new FPDF();
$pdf->AddPage();

//Now show the 3 columns
$pdf->SetFont('Arial','',12);
$pdf->SetY(26);
$pdf->SetX(45);
$pdf->MultiCell(20,6,$c_code,1);
$pdf->SetY(26);
$pdf->SetX(65);
$pdf->MultiCell(100,6,$c_name,1);
$pdf->SetY(26);
$pdf->SetX(135);
$pdf->MultiCell(30,6,$c_price,1,'R');
$pdf->SetX(135);
$pdf->MultiCell(30,6,'$ '.$total,1,'R');

$filename="invoice.pdf";
$pdf->Output($filename,'F');

echo'<a href="invoice.pdf">Download your Invoice</a>';

?>

上面的代码给了我这样的结果-

 ----------------------------------
| 1  | ABC                | 1000   |
| 2  | XYZ                |  500   |
| 3  | PZX                |  100   |
 ----------------------------------
                          | 1600   |
                           --------

我希望我的结果像这样显示

 ----------------------------------
| 1  | ABC                | 1000   |
| 3  | PZX                |  100   |
 ----------------------------------
                          | 1100   |
                           --------

再次提前感谢..

【问题讨论】:

    标签: php mysql sql pdf fpdf


    【解决方案1】:

    以下代码在对上述代码进行少量修改后运行良好(使用 where 子句)

    $result=mysql_query("select * from `receipt` where id IN(1,3) "); 
    

    一个变量可以通过使用 implode 分隔来使用尽可能多的变量,例如从上一页选择的复选框数组值

    <?php
    
    require('u/fpdf.php');//fpdf path
    include_once('db_connect.php');// my db connection
    
    $sel =array (1,3);// can use $sel =$_GET['sel']; where $_GET['sel'], sel is a checkbox array value received from prev page..
    
    $id=implode(",",$sel);//seperating ',' from array
    
    $result=mysql_query("select * from `receipt` where id IN($id) ");
    //Initialize the 3 columns and the total
    $c_code = "";
    $c_name = "";
    $c_price = "";
    $total = 0;
    
    //For each row, add the field to the corresponding column
    while($row = mysql_fetch_array($result))
    { 
       $code =$row['id'];
       $name = substr($row['company'],0,20);
       $real_price = $row['total'];
       $show =$row['total'];
    
     $c_code = $c_code.$code."\n";
     $c_name = $c_name.$name."\n";
     $c_price = $c_price.$show."\n";
    
    //Sum all the Prices (TOTAL)
        $total = $total+$real_price;
    }
    mysql_close();
    
    $total = $total;
    
    //Create a new PDF file
    $pdf=new FPDF();
    $pdf->AddPage();
    
    //Now show the 3 columns
    $pdf->SetFont('Arial','',12);
    $pdf->SetY(26);
    $pdf->SetX(45);
    $pdf->MultiCell(20,6,$c_code,1);
    $pdf->SetY(26);
    $pdf->SetX(65);
    $pdf->MultiCell(100,6,$c_name,1);
    $pdf->SetY(26);
    $pdf->SetX(135);
    $pdf->MultiCell(30,6,$c_price,1,'R');
    $pdf->SetX(135);
    $pdf->MultiCell(30,6,'$ '.$total,1,'R');
    
    $filename="invoice.pdf";
    $pdf->Output($filename,'F');
    
    echo'<a href="invoice.pdf">Download your Invoice</a>';
    
    ?>
    

    【讨论】:

      【解决方案2】:
      $result=mysql_query("select * from `receipt` where id IN(1,3) ");
      

      【讨论】:

      • 感谢您的支持,但 order 用于排列数据(如果我是正确的,则为 mysql),您的查询仅按字母顺序排列我的数据,我想缩短 table.id 应该为 1 的数据和 3,这样我只能得到 2 个结果,一个用于第 1 行,另一个用于第 3 行。
      • 感谢您的建议,我在下面上传了完整的功能代码。
      猜你喜欢
      • 1970-01-01
      • 2015-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多