gpt4 book ai didi

php - FPDF,从 mysql 表生成选定值的 pdf

转载 作者:行者123 更新时间:2023-11-29 13:26:31 25 4
gpt4 key购买 nike

你好 friend ,下面是我的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 |
--------

再次提前致谢..

最佳答案

对上述代码进行少量修改后,以下代码可以正常运行(使用 where 子句)

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

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

<?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>';

?>

关于php - FPDF,从 mysql 表生成选定值的 pdf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20055649/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com