Search code examples
phpmysqlfpdf

How to use variables outside the loop in FPDF


I am generating a PDf doc using FPDF, when I click send button on a row in a table, I wanted to Generate a PDF for that Record and send it to the record email. The PDF gets Generated normally, but I want to use a Variable "email" of each record to send to that record .. but On $to, I couldn't use email variable from my database.

I tried all combination of things and I am missing something for sure. The $idx is an Id from previous page (Index.php) posting data to "mail.php) where my PDF gets generated.

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

Variables outside the while loop are consifered unidentified!


Solution

  • I did manage to make it work:

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