FPDF动态table位置

FPDF dynamic table position

我正在使用这个 script 来创建一个包含多个 table 的 pdf。

首先,我从 table 中获取员工列表,然后,对于该列表中的每个元素,我想将员工姓名和 table 发送到 pdf 文件他的信息。 我的问题是我认为 Y 位置在第一页上工作正常,但在那之后 tables 没有正确显示。任何帮助将不胜感激...

这是我的代码:

<?php
require('fpdf.php');

$pdf=new FPDF();
$pdf->AliasNbPages();
$pdf->AddPage();

$Y = 80;
$Y_Fields_Name_position = 90;
$Y_Table_Position = 96;


$get_data = $mysqli->query("SELECT name FROM employees ORDER BY name"); 
if ($get_data) {
while ($data = $get_data->fetch_array(MYSQLI_BOTH)) {

    $result = $mysqli->query("select departamento as Departamento, categoria as Categoria, count(*) as Totales from gestiones
                                        where date(fecha) between '2015-01-01' and '2015-01-31' and employee = '$data[0]'
                                        group by categoria
                                        order by Totales desc");    
    $number_of_products = $result->num_rows;

    if ($number_of_products > 0) {

        $column_departamento = "";
        $column_categoria = "";
        $column_totales = "";
        $total = 0;

        if ($result) {
                    while ($row = $result->fetch_row()) {
                    $departamento=$row[0];
                    $categoria=utf8_decode($row[1]);
                    $totales=$row[2];

                    $column_departamento = $column_departamento.$departamento."\n";
                    $column_categoria = $column_categoria.$categoria."\n";
                    $column_totales = $column_totales.$totales."\n";

                    //Sum totales
                    $total += $row[2];

                }
            }

        $pdf->SetFont('Arial','B',12);
        $pdf->SetY($Y);
        $pdf->Cell(40,10,$data[0]);

        $pdf->SetFillColor(232,232,232);
        $pdf->SetY($Y_Fields_Name_position);
        $pdf->SetX(20);
        $pdf->Cell(30,6,'Depto.',1,0,'C',1);
        $pdf->SetX(50);
        $pdf->Cell(110,6,utf8_decode('Categoría'),1,0,'L',1);
        $pdf->SetX(160);
        $pdf->Cell(30,6,'Totales',1,0,'C',1);
        $pdf->Ln();

        $pdf->SetFont('Arial','',11);
        $pdf->SetY($Y_Table_Position);
        $pdf->SetX(20);
        $pdf->MultiCell(30,6,$column_departamento,1,'C');
        $pdf->SetY($Y_Table_Position);
        $pdf->SetX(50);
        $pdf->MultiCell(110,6,$column_categoria,1);
        $pdf->SetY($Y_Table_Position);
        $pdf->SetX(160);
        $pdf->MultiCell(30,6,$column_totales,1,'C');
        $pdf->SetX(160);
        $pdf->MultiCell(30,6,$total,1,'C');

        $Y = $Y + 90;
        $Y_Fields_Name_position = $Y_Fields_Name_position + 90;
        $Y_Table_Position = $Y_Table_Position + 90;

        $gran_total += $total;

    }

}

$start_Y= $pdf->GetY();
$pdf->SetFont('Arial','B',15);
$pdf->SetY($start_Y + 30);
$pdf->Cell(40,10,'Total de Servicios: '.$gran_total);

}

$pdf->Output();

$result->free();
$get_data->free();
$mysqli->close();
?>

提前致谢。

我添加了这样的东西,它似乎工作正常:

$start_Y = $pdf->GetY();

if  ($start_Y > 200) {

    $pdf->AddPage();
    $Y = 10;
    $Y_Fields_Name_position = 20;
    $Y_Table_Position = 26;

} else {

    $Y = $start_Y + 1;
    $Y_Fields_Name_position = $start_Y + 11;
    $Y_Table_Position = $start_Y + 17;
}

谢谢!