我正在使用 FPDF 生成 PDF 文档,当我单击表中一行上的发送按钮时,我想为该记录生成 PDF 并将其发送到记录电子邮件。 PDF 正常生成,但我想使用每个记录的变量“电子邮件”发送到该记录..但是在 $to 上,我无法使用数据库中的电子邮件变量。
我尝试了所有的组合,但我肯定错过了一些东西。 $idx 是上一页 (Index.php) 中的 ID,将数据发布到生成 PDF 的“mail.php”。
<!-- Index.php -->
<a class = "btn btn-info pid"
href = "mailto.php?idx=<?php echo $f_staff['id']?>" data-toggle="tooltip" title="Email This">
<span class="glyphicon glyphicon-envelope"></span>
</a>
<!-- mailto.php -->
<?php
require('../fpdf/fpdf.php');
$db = new PDO('mysql:host=localhost;dbname=mcle','root','PASS');
// CREATE AND SAVE THE PDF DOCUMENT
class pdf extends FPDF
{
function header()
{
Bla Bla Bla Bla.!
}
function viewTable($db)
{
$this->SetFont('Times','',12);
$idx = $_GET['idx'];
$stmt = $db->query("SELECT
staff.id,
staff.fname,
staff.lname,
staff.staff_bar_no,
staff.email
FROM staff JOIN attendance ON staff.staff_bar_no = attendance.staff_bar_no
WHERE attendance.event_id = 13 AND staff.id = '$idx'");
while($data = $stmt->fetch(PDO::FETCH_OBJ))
{
(This part WORKS fine with $data->Somevariables...
$this->Cell(80,6,$data->fname." ".$data->lname ,0,0,'L');
Bla Bla Bla Bla.!
}
}
}
$pdf = new pdf();
$pdf->AddPage('P','A4',0);
$pdf->view_pre_Table($db);
$pdf->viewTable($db);
// email stuff (change data below)
This is where I wanted to use Email Variable from the Array above... Tried $data->email; didn't work
$to = "email@email.net";
Bla Bla Bla Bla.!
// send message
mail($to, $subject, $body, $headers);
?>
while 循环之外的变量被视为未识别!
最佳答案
我没有保存在 php 中,这样的东西可以工作:
<!-- mailto.php -->
<?php
require('../fpdf/fpdf.php');
$db = new PDO('mysql:host=localhost;dbname=mcle','root','PASS');
// CREATE AND SAVE THE PDF DOCUMENT
class pdf extends FPDF
{
//declare empty array
public $datas = [];
function header()
{
Bla Bla Bla Bla.!
}
function viewTable($db)
{
$this->SetFont('Times','',12);
$idx = $_GET['idx'];
$stmt = $db->query("SELECT
staff.id,
staff.fname,
staff.lname,
staff.staff_bar_no,
staff.email
FROM staff JOIN attendance ON staff.staff_bar_no = attendance.staff_bar_no
WHERE attendance.event_id = 13 AND staff.id = '$idx'");
while($data = $stmt->fetch(PDO::FETCH_OBJ))
{
array_push(this->datas,$data); //push each data into array
(This part WORKS fine with $data->Somevariables...
$this->Cell(80,6,$data->fname." ".$data->lname ,0,0,'L');
Bla Bla Bla Bla.!
}
}
}
$pdf = new pdf();
$pdf->AddPage('P','A4',0);
$pdf->view_pre_Table($db);
$pdf->viewTable($db);
// email stuff (change data below)
// now you can iterate over $pdf->datas
// send message
mail($to, $subject, $body, $headers);
?>
关于php - 如何在 FPDF 中使用循环外的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58601708/