My first php question…
I’m using fpdf to generate pdf files. The original version of the site I’m doing used a generic image on the print out. I want to change this to an id-based image.
My problem is the header with the image is declared before the query that retrieves the ID runs. This is probably easy to fix, but not with zero php-exp…
Here is my code:
<?php
require('fpdf.php');
import_request_variables("GP", "rvar_");
$prn_con = mysql_connect('localhost', 'root', '');
mysql_select_db('db');
class PDF extends FPDF{
function Header(){
$this->Image('../../images/logo_generic.gif',170,8,30);
}
}
$pdf=new PDF();
$pdf->Open();
$tok = strtok($rvar_bestellkeys,",");
while ($tok){
$result = mysql_query('SELECT * FROM bk WHERE bestellkey = "'.$tok.'"');
$i = 0;
$fCount = mysql_num_fields($result);
while($row=mysql_fetch_row($result)){
while ($i < $fCount){
$fName = mysql_field_name($result, $i);
$$fName = trim($row[$i]);
$i++;
$strcont = strlen ($$fName);
}
$i=0;
}
One of query results is the ID I’m after to include in the header() logo path.
Question:
How can I set the header() path from inside my while-loop? The loop prints out documents (single/multipage) and every page should have the logo top-right.
Thanks for help!
EDIT:
Here is the whole php script:
<?php
require('fpdf.php');
import_request_variables("GP", "rvar_");
$prn_con = mysql_connect('localhost', 'root', '');
mysql_select_db('db');
$resultsprache = mysql_query("SELECT * FROM sprachen WHERE sprache = '".$rvar_sprache."'");
while($row=mysql_fetch_row($resultsprache)){
$fName = trim($row[2]);
$$fName = trim($row[3]);
}
$FileN = $tx_bestellung. '_'.date("YmdHis").'.pdf';
$title = $tx_bestellung;
class PDF extends FPDF{
function Header(){
$this->Image('../../images/logo_klein.gif',170,8,30);
}
}
$pdf=new PDF();
$pdf->Open();
$pdf->AliasNbPages();
$pdf->AddPage();
$pdf->SetTitle($title);
$pdf->SetFont('Arial','',8);
$tok = strtok($rvar_bestellkeys,",");
while ($tok){
$result = mysql_query('SELECT * FROM bK WHERE bestellkey = "'.$tok.'"');
$i = 0;
$fCount = mysql_num_fields($result);
while($row=mysql_fetch_row($result)){
while ($i < $fCount){
$fName = mysql_field_name($result, $i);
$$fName = trim($row[$i]);
$i++;
$strcont = strlen ($$fName);
}
$i=0;
}
$pdf->Cell(190,7,$tx_bestellung. " " .$tx_nr. " " .$bestellkey,0,1,'L');
$pdf->Ln(8);
$pdf->Cell(190,5,$tx_verkaeufer,0,1);
$pdf->Cell(30,4,'ILN/GLN:',0,0);
$pdf->Cell(65,4,$vk_iln,0,1);
$pdf->Cell(30,4,$tx_firma. ':',0,0);
$pdf->Cell(65,4,$vk_firma,0,0);
$pdf->Cell(30,4,$tx_bestellnummer. ':',0,0);
$pdf->Cell(65,4,$bestellkey,0,1);
$pdf->Cell(30,4,$tx_adresse. ':',0,0);
$pdf->Cell(65,4,$vk_adresse,0,0);
$pdf->Cell(30,4,$bestelldatum. ':',0,0);
$bestelldatum = (substr($bestelldatum, 8,2)).'.'.(substr($bestelldatum, 5, -3)).'.'.(substr($bestelldatum, 0, -6));
$pdf->Cell(65,4,$bestelldatum,0,1);
$pdf->Cell(30,4,$tx_plz. ', ' .$tx_ort. ':',0,0);
$pdf->Cell(65,4,$vk_plz.' '.$vk_ort,0,0);
....
$i=0;
$result = mysql_query('SELECT * FROM bP WHERE bestellkey = "'.$tok.'"');
$fCount = mysql_num_fields($result);
$pdf->Cell(9,8,$tx_pos,1,0,'C');
$pdf->Cell(23,8,"EAN/GTIN",1,0,'C');
$pdf->Cell(43,8,$tx_bezeichnung,1,0,'C');
$pdf->Cell(10,8,$tx_groesse,1,0,'C');
$pdf->Cell(10,8,$tx_farbe,1,0,'C');
$pdf->Cell(15,8,$tx_preis,1,0,'C');
$pdf->Cell(12,8,$tx_einheit,1,0,'C');
$pdf->Cell(15,8,$tx_menge,1,0,'C');
$pdf->Cell(15,8,$tx_menge. "\n" .$tx_bestaetigt,1,0,'C');
$pdf->Cell(18,8,$tx_summe,1,1,'C');
...
$tok = strtok(",");
if($tok != ""){
$pdf->AddPage();
}
}
mysql_close($prn_con);
$pdf->Output($FileN, 'D');
?>
Quick and pretty dirty, but should work: