2013-03-12 123 views
0

我有一些麻烦,得到一个SQL查询工作内部fpdf,这甚至有可能吗?虽然在FPDF循环

我现在有

session_start(); 
require('fpdf.php');  
class PDF extends FPDF 
{ 
    function Header() 
     { 
      include('config.php'); 
      $client_check = $db->prepare("SELECT * FROM clients WHERE client_fullname = '".$_SESSION['client_details']."'"); 
      $client_check->execute(); 
      while ($row = $client_check->fetch(PDO::FETCH_ASSOC)) 
      { 
       $client_firstname = $row ['client_firstname']; 
       $client_lastname  = $row ['client_lastname']; 
       $client_address  = $row ['client_address']; 
       $client_jobaddress = $row ['client_jobaddress']; 
       $client_homephone = $row ['client_homephone']; 

       $this->SetFont('Arial', 'B', 12); 
       $this->Cell(10,0,'Ph(H):',0,0,'C'); 
       $this->Cell(20,0,''.$client_homephone.'', 0,0,'C'); 
       $this->Line(30,61,100,61); 
       $this->Cell(210,0,'Job No:',0,0,'C'); 
       $this->Cell(-120,0,'Model:',0,0,'C'); 
       $this->Line(110,61,200,61); 
      } 
     } 
    } 
// Instanciation of inherited class 
$pdf = new PDF(); 
$pdf->AliasNbPages(); 
$pdf->AddPage(); 
$pdf->SetFont('Times','',10); 
$pdf->Output(); 

一切工作很好地之前,我添加了while循环。现在它只是向我吐出一张空白的pdf。我怎样才能解决这个问题?

+0

能否请你告诉我你的实际需求是什么,所以我可以建议你一个更好的方法。 – 2013-03-12 04:28:00

+0

@Praveenkalal我需要进行SQL调用才能从数据库中获取数据以添加到PDF中。 – 2013-03-12 04:35:08

+1

请先检查循环是否正常工作,然后再动态改变位置,否则每次都会覆盖文本。 – 2013-03-12 04:39:24

回答

0
$con = mysql_connect("localhost", "{username}", "{password}") or die(mysql_error()) ; 
if (!$con) 
    { 
    die('Could not connect: ' . mysql_error()); 
    } 
$db = mysql_select_db("{db}", $con) or die(mysql_error()) ; 
$client= mysql_query("{SELECT * FROM clients}")or die(mysql_error()); 
while ($client2= mysql_fetch_array($client)){ 

$pdf = new PDF(); 
$pdf->AliasNbPages(); 
$pdf->AddPage(); 
$pdf->SetFont('Times','',10); 

$pdf->Cell(20,0,''.$client2["client_homephone"].'', 0,0,'C'); 
..... 

} 

mysql_close($con); 

$pdf->Output(); 
+0

欣赏答复,但不应使用mysql_ *函数。请参阅http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php – 2013-03-14 04:33:17

1

恐怕你不能做到这一点

这是因为你的头功能运行每次添加新的页面时(这是自动完成的,如果一个页面溢出)到PDF

相反,你可以像这样

  1. 创建一个构造的PDF类调用一个函数说Fill_details
  2. 移动while循环进入功能Fill_details
  3. 页眉和页脚功能应该包含通用于所有的网页说,像页面边框,页脚版权的代码的那一部分,等