【问题标题】:While loop in FPDFFPDF中的while循环
【发布时间】:2023-09-12 13:12:02
【问题描述】:

我在让 SQL 查询在 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。我该如何解决这个问题?

【问题讨论】:

  • 你能告诉我你的实际要求是什么,我可以给你一个更好的建议。
  • @Praveenkalal 我需要进行 SQL 调用以从数据库中获取数据以添加到 PDF。
  • 请先检查循环是否正常工作,然后动态更改位置,否则每次都会覆盖文本。
  • FPDF 可以实现您所尝试的一切。你需要检查你的循环。
  • 您是否在检查 $client_check->execute 附近的查询是否正确执行??

标签: php fpdf


【解决方案1】:
$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();

【讨论】:

【解决方案2】:

恐怕你做不到

这是因为每次将新页面添加到 pdf 时都会运行您的 Header 函数(如果页面溢出,则会自动完成)

相反,你可以这样做

  1. 为调用 Fill_details 等函数的 PDF 类创建一个构造函数
  2. 将while循环移到函数Fill_details中
  3. 页眉和页脚函数应仅包含所有页面共有的代码部分,例如页面边框、页脚版权等

【讨论】: