Dompdf while 循环 - 仅最后一条记录转换为 pdf

问题描述 投票:0回答:1
<?php require"../db_connection1.php";?>
<?php require"../db_connection4.php";?>
<?php require"../db_connection5.php";?>
<?php

require_once('../dompdf/autoload.inc.php') ;
use Dompdf\Dompdf ;
ob_start() ;

if (isset($_POST["submit"])) {  
    $payroll_date    = mysqli_real_escape_string($connection5, $_POST["payroll_date"]) ;
}


$query = "SELECT * FROM  ".$payroll_date."  ";
$result = mysqli_query($connection5, $query);
while ($row1 = mysqli_fetch_array($result)) {   
    $Employee_Number   = $row1[0] ;
}


?>           
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title></title>
  <link href="../bootstrap.min.css" rel="stylesheet">
  <style type="text/css">
    .SECTION-1 { 
      margin-top:110px ;    
      margin-left: 108px ;
      margin-bottom: 5px ;
    }
  </style>
</head> 
<body>
  <form action = "#" method ="POST">
  <div class ="SECTION-1" > 

  <?php

  $query_hr_data = "SELECT * FROM applicants_final WHERE employee_number = '{$Employee_Number}' ";

  $result = mysqli_query($connection4, $query_hr_data );
  while ($row5 = mysqli_fetch_array($result)) {

    $cost_center         =   $row5['cost_center'] ;
    $position            =   $row5['position'] ;

    $query = "SELECT * FROM  ".$payroll_date."  WHERE EE_NUMBER = '{$Employee_Number}' ";
    $result = mysqli_query($connection5, $query);

    while ($row1 = mysqli_fetch_array($result)) {

      $EE_NUMBER         =   $row1['EE_NUMBER'] ;
      $EE_NAME           =   $row1['EE_NAME'] ;

      $query2 = "SELECT * FROM  ".$payroll_date."  WHERE EE_NUMBER = '{$Employee_Number}' ";
      $result = mysqli_query($connection1, $query);

      while ($row2 = mysqli_fetch_array($result)) {

        $LATES_UNDERTIME_HRS   =   $row2['LATES_UNDERTIME'] ;
        $ABSENCES_HRS          =   $row2['ABSENCES'] ;



        echo "<table id ='heading'>" ;

        echo"<tr>" ;
        echo"<th id ='heading-2-1'>EE Name </th>" ;
        echo"<td id ='heading-2'>" .$row1['EE_NAME']." </br> </td>" ;
        echo"<th id ='heading-2-1'>Philhealth # </th>" ;
        echo"<td id ='heading-2'>" .$philhealth_number." </br> </td>" ;
        echo"</tr>" ;

        echo"<tr>" ;
        echo"<th id ='heading-2-1'>Basic_Salary </th>" ;
        echo"<td id ='heading-2'>" .number_format($basic_salary,2). "</br> </td>" ;
        echo"<th id ='heading-2-1'> Allowances </th>" ;
        echo"<td id ='heading-2'>" .number_format($deminimis_benefits,2)."</br> </td>" ;
        echo"</tr>" ;

        echo "</table>" ;
        echo "<table id='design-8'>" ;

        echo"<tr>" ;
        echo"<th>Lates_Undertime ( minutes ) </th>" ;
        echo"<td>" .$LATES_UNDERTIME_HRS. "</br> </td>" ;
        echo"<td>" .$LATES_UNDERTIME." </br> </td>" ;
        echo"</tr>" ;

        echo"<tr>" ;
        echo"<th>Absences </th>" ;
        echo"<td>" .$ABSENCES_HRS." </br> </td>" ;
        echo"<td>" .$ABSENCES." </br> </td>" ;
        echo"</tr>" ;


        echo "</table>" ;
        echo "<div id='design-2'> *** Payslip Confidential *** </div>" ;

  ?>        
  </form>
  </div>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
</body>
</html>

    <?php
    $html = ob_get_clean();
    $dompdf = new Dompdf();
    $dompdf->load_html($html);
    $dompdf->setPaper(array(0, 0, 612, 1100), 'portrait');
    $dompdf->render();

    $pdf = $dompdf->output();
    $file_location = "C:/wamp/www/dbs-program/payroll-program/version-3.4/pdfReports/".$Employee_Number.".pdf" ;
    file_put_contents($file_location, $pdf);

    }

  }

}

?>

谢谢布莱恩,我更新了代码,我只是删除了冗余数据,这样就可以了 不要太长。再次感谢您再次查看。

我想打印 50 多个 pdf 并将其保存到指定文件夹,pdf 名称为员工编号,来自 mysql数据库。现在的问题只是 while 循环中的最后一个数据正在被 转换为pdf,如果我将第一个查询的右括号转移到 html后的最后一个代码,转换后的pdf只是第一个。当我 echo $Employee_Number = $row1[0] 我得到了所有 50 多个数据。我已经尝试过了 迭代,例如 for,但它变得更复杂,因为它打印出来 pdf 的确切数量,但没有打印任何数据,希望有人 有关如何继续的帮助或提示。先谢谢你了。

php dompdf
1个回答
0
投票
I also have a similar problem.only the last record in the  database gets converted into pdf.anyone to help.
<?php
`require_once('./dompdf/dompdf/autoload.inc.php');
include('connect.php');    
use Dompdf\Dompdf;`

`$sql ="SELECT fname,lname FROM worker";
$results= mysqli_query($conn,$sql);
while($row=mysqli_fetch_assoc($results)){
$fname=$row['fname'];
$lname=$row['lname'];
  $i=1;
  $html='';
$html .='
<table border="1" cellspacing="0"style="width:100%; text-align:center" >
<tr>
<th>#</th>
<th>FIRST NAME</th>
<th>LAST NAME</th>
</tr>
 <tr>
<td >'.$i.'</td>
<td> '.$fname.'</td>
<td>'.$lname.'</td>
        </tr>
</table>
';
$i++;
}

$dompdf= new dompdf();
$dompdf->loadhtml($html);
$dompdf->setPaper('A4','landscape');
$dompdf->render();
$dompdf->stream("liseli",array("Attachment"=>0));`


?>
© www.soinside.com 2019 - 2024. All rights reserved.