0

I'm trying to generate a pdf using the FPDF class but the problem I'm having is how to pass variable data fetched from mysql database (queried data are stored in an array) into the pdf generated.

Here are the codes in the script meant to generate the pdf

        <?php
        @include_once("includes/db.php");
        require('fpdf/fpdf.php');
        @include_once("includes/course_info_query.php");

        $obj = new trainingCourses();
        $course_details = array();
        if(isset($_GET['c_id'])){
                $course_details = $obj->getPubCourseDetails($_GET['c_id']);
        }




        class PDF extends FPDF
        {   


    public $course_details;

    public function setData($input){
        $this->course_details = $input;
    }


        function Header()
        {
            // Logo
            $this->Image('s_pdf_logo.png',10,6,30);
            // Arial bold 15
            $this->SetFont('Arial','B',20);
            // Move to the right
            $this->Cell(40);    
            // Title
            $this->Cell(30,10,$this->course_details['comp_name']);
            // Draw an header line
            $this->Line(10,26,200,26);
            // Line break
            $this->Ln(20);
        }

        function Footer()
        {
            // Position at 1.5 cm from bottom
            $this->SetY(-15);

            // Begin with regular font
            $this->SetFont('Arial','',9);
            // Then put a blue underlined link
            //$this->SetTextColor(0,0,255);
            $this->SetFont('','U');
            $this->Write(10,$this->course_details['comp_name'],'http://www.skills.com');
            // Arial italic 8
            $this->SetFont('Arial','I',9);
            // Page number
            $this->Cell(0,10,'Page '.$this->PageNo().' ',0,0,'R');
        }


        function BodyTop()
        {
        // Course title cell
        $this->Cell('',10,'',0,0,'C');
        $this->Ln();

        /* Build cells for the first row */

        $this->SetFont('Arial','',10);
        $this->SetY(40);

        // First Row
        $this->Cell(35,8,'Starting Date : ',0,0,'L');
        $this->Cell(30,8,$this->course_details['event_date'],0,0,'C');
        $this->SetX(150);
        $this->Cell(25,8,'Course Fee : ',0,0,'L');
        $this->Cell(20,8,$this->course_details['fee'],0,0,'C');
        $this->Ln();

        // Second Row
        $this->Cell(35,8,'Seating Capacity : ',0,0,'L');
        $this->Cell(30,8,$this->course_details['sit_capacity'],0,0,'L');
        $this->SetX(150);
        $this->Cell(25,8,'Duration : ',0,0,'L');
        $this->Cell(20,8,$this->course_details['duration'].' Day(s)',0,0,'L');
        $this->Ln();

        // Third Row
        $this->Cell(35,8,'Venue : ',0,0,'L');
        $this->Cell(30,8,$this->course_details['venue'],0,0,'L');
        $this->SetX(150);
        $this->Cell(25,8,'City : ',0,0,'L');
        $this->Cell(20,8,$this->course_details['city'],0,0,'L');
        $this->Ln();

        // Fourth Row
        $this->Cell(35,8,'Other Details : ',0,0,'L');
        $this->Cell(150,8,$this->course_details['other_det'],0,0,'L');
        $this->Ln();


        }


        function CourseBody()
        {

        $this->SetY(80);

        //$this->WriteHTML($html);
        $this->Write(10,$this->course_details['desc']);

        }

        function PrintChapter()
        {
            $this->AddPage();
            $this->BodyTop();
            $this->CourseBody();
        }
        }

        $pdf = new PDF();

$pdf->setData($course_details);
        //$pdf->Header();

        $pdf->SetAuthor($this->course_details['comp_name']);
        $pdf->PrintChapter();
        $pdf->Output();



        ?>

I hope to get some help with this...Thanks

1
  • 2
    Call a method and pass the data as parameter...? Commented Nov 22, 2012 at 10:43

1 Answer 1

4

It's just basic OOP (object oriented programming) really.

Add a variable to your PDF Class as such:

public $data;

Add a function within your PDF Class which accepts a parameter and use this function to set the variable above:

public function setData($input){
    $this->data = $input;
}

And then you'll be able to access $this->data from within your PDF Class. Don't forget to actually call the function that you just defined (just after the constructor).

EDIT: To set the data:

$pdf = new PDF();
$pdf->setData($course_details);

Then within your PDF class $this->data will be your array. You might want to do the following so you can understand the format of your array:

print_r($this->data);
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for your help but still getting an error message 'Cannot access empty property'...this is refering to the '$course_details' array holding the data. Just to let you know I'm a procedural PHP guy, and just started learning OOP...I hope to get more help from you...Thanks!
Added a couple more code samples to my answer. Objects you define from outside of a class can't be accessed from within. They're seen on a separate level (read up on object scope) from one another. That is why I was showing you how to pass the variable into the class. $this->data then becomes your array.
I have tried displaying the variables (elements of the array) by using $this->data['duration'] but not working...'duration' is the name of the column in the database...Kindly show me how you would display the element of the array within the function to output in the pdf... Example: $this->Cell(20,8,$this->data['duration']' Day(s)',0,0,'L');
There was an error in your line of code (the . after the array - combines the two strings): $this->Cell(20,8,$this->data['duration'].' Day(s)',0,0,'L');
Yea there's an error but could you show me how you will place the variable element from the array inside the function. Here is another line of code I used while trying to place the variable: Example: $this->Cell(20,8,$this->data['city'],0,0,'L'); I appreciate your help so far, but what I need is how to place the variable and then generate the pdf to see if it's working.....Thanks!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.