尝试在 PDF 上打印 2 列中的 2 个数组,但第一个数组打印正确,而第二个数组打印聚集的值彼此重叠


$sql = " SELECT Salary,Bonus,Housing_Allowance,Transport_Allowance,Travel_Allowance,Vehicle_Allowance,Cellphone_Allowance,Entertainment_Allowance,Company_Car,Medical_Allowance FROM $tb2_name WHERE Month='$month' AND Year='$year' AND Employee_Number='$user'";
$result = mysqli_query($conn, $sql);
$rows = mysqli_fetch_assoc($result);
// This code works fine as the array are printed under each other .  thanks to the n
$data = array();
foreach($rows as $key => $value) {
  if($value > 0) {
    $me =  $key . " : " . $value . "n";
    $pdf->MultiCell(63, 10, $me, 2);
  }
}

上面的代码非常有效,因为数组值在 pdf 页面上彼此打印在一起而不是聚类。 当我尝试在旁边的列中执行相同的操作时,下一段代码会出现

问题
$pdf->SetXY($x + 35, $y);
$col2="Deductions  Other Deductions   ";

$col3="Deductionsn n  $DSocial_Security  n $DContributions n $DVehicle_Allowance n $Other_Deductions";
$wql = " SELECT Housing_Allowance,Vehicle_Allowance,Company_Car,DContributions,Pension, Provident_Fund,Retirement_Annuity,Study_Policy FROM $tb2_name WHERE Month='$month' AND Year='$year' AND Employee_Number='$user'";
$results = mysqli_query($conn, $wql);
$rows1 = mysqli_fetch_assoc($results);

问题出在这里。当执行此操作时,结果将打印在彼此之上,并聚集在一起,而不是像所说的那样相互簇。

$data1 = array();
foreach($rows1 as $key => $value) {
  if($value > 0) {
    $me =  $key . " : " . $value . "n";
     $pdf->SetXY($x + 60 ,$y);
    $pdf->MultiCell(63, 10, $me, 3);
    }

  }
the above code does not print the array so that the values line up under each other but the values line up on top of each other and are clustered.

$pdf->SetXY($x + 95 ,$y);
$pdf->SetXY($x + 120 ,$y);
$pdf->MultiCell(63, 10, $col2, 3);

根据我根据您的代码/语句 请增加您的$x定位,如下所示:

$data1 = array();
foreach($rows1 as $key => $value) {
  if($value > 0) {
    $me =  $key . " : " . $value . "n";
     $pdf->SetXY($x ,$y);
     $pdf->MultiCell(63, 10, $me, 3);
     $x= $x + 5;  // insert this line 
    }
  }

首先,代码MultiCell默认情况下会增加x,因此它可以完美地工作。在第二个代码中,您指定了 SetXY() 进行定位,因此每次您必须以所需的边距增加 X 仓位......感谢

最新更新