【发布时间】: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 |
--------
再次提前感谢..
【问题讨论】: