Search code examples
phpfpdf

Adding $_POST to FPDF files, or connect database to FPDF?


I want to do this:

When user edits some text and clicks submit - for html to $_post the info into a FPDF file.

I'm stuck here:

<?php
require('fpdf.php');

$pdf=new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Hello World!');
$pdf->AddPage();
$pdf->Cell(60,10,'Powered by FPDF.',0,1,'C');

$pdf-> Output();
?>

How can I add a $_POST attribute? If I do, it gives an error.

Or if someone knows a way to connect to the database and display info from tables... that would be nice.

Example I found:

$query = "SELECT imageField FROM yyy WHERE ...";
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);
$image = $row['imageField'];

$pdf->MemImage($image, 50, 30);

Obviously that is an image... how can it be done with text?


Solution

  • I'm assuming you are copying and pasting code snippets to make the thing work. Suppose the post variable 'name' is set to 'MyName' by you, then you can print it by contactinating it with the string you pass to the function :

    $pdf->Cell(40,10,'Hello World - '.$_POST[name]);
    

    This would print 'Hello World - MyName' to the PDF file.

    To connect to a MYSQL database, you need the following statements:

    mysql_connect('localhost','username','password');  //Replace 'username' and 'password' by your mysql username and password
    
    mysql_select_db('db');  //Replace 'db' by the name of the database
    

    Now make the SQL query to select the field fieldname from the table table in the database db mentioned above. The condition decides which rows are selected. If you keep condition as 1, all rows are selected. You can use a condition like field2='somevalue' to select only those rows where the value of field2 is somevalue.

    $query=mysql_query("SELECT *fieldname* FROM table WHERE condition "); 
    

    To retrieve the result use the function mysql_fetch_assos():

    $row_array= mysql_fetch_assos($query);
    

    mysql_fetch_assos() returns an assosiative array containing the field values of a particular row and moves the resource pointer by one.

    $field_value=$row_array['field2'];