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

时间:2019-10-29 05:09:00

标签: php mysql fpdf

我正在使用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循环之外的变量被认为是未知的!

3 个答案:

答案 0 :(得分:1)

我没有保存在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);
        ?>

答案 1 :(得分:0)

这是我的代码:

$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>";

答案 2 :(得分:0)

我确实做到了:

$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>";