如何在 FPDF 的循环外使用变量

How to use variables outside the loop in FPDF

我正在使用 FPDF 生成 PDF 文档,当我单击 table 中某行的发送按钮时,我想为该记录生成 PDF 并将其发送到记录电子邮件。 PDF 正常生成,但我想使用每条记录的变量 "email" 发送到该记录。但是在 $to 上,我无法使用数据库中的电子邮件变量。

我尝试了所有组合,但肯定遗漏了一些东西。 $idx 是来自上一页 (Index.php) 的 Id,将数据发布到“mail.php) 生成我的 PDF。

<!-- 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);
        ?>

这是我的代码:

$pdf = new pdf();
$pdf->AddPage('P','A4',0);
$pdf->view_pre_Table($db);
$pdf->viewTable($db);
//$pdf->Output(); //This is if you want an Output to the screen Rather than emailing..

$emailto = $_GET['idx'];// changed this it was base in id, changed to email instead and 
assigned it to $emailto, then used it to get mail recipients.

// email stuff (change data below)
$to = $emailto;
$from = "LSNC MCLE"; 
$subject = "Your LSNC MCLE Attendance Certificate"; 
$message = "<p>Please see the attachment.</p>";

我确实成功了:

$pdf = new pdf(); 
$pdf->AddPage('P','A4',0); 
$pdf->view_pre_Table($db); 
$pdf->viewTable($db); 
//$pdf->Output(); 
$emailto = $_GET['idx']; // email stuff (change data below) 
$to = $emailto; 
$from = "LSNC MCLE"; 
$subject = "Your LSNC MCLE Attendance Certificate"; 
$message = "<p>Please see the attachment.</p>";