How can the GetY function be effectively used in FPDF to create a dynamic table layout?

To create a dynamic table layout using FPDF, the GetY function can be used to determine the vertical position where the next element should be placed. By utilizing GetY, you can easily position table rows below each other without having to manually calculate the Y coordinate for each row.

// Initialize FPDF
require('fpdf.php');
$pdf = new FPDF();
$pdf->AddPage();

// Define table headers and data
$headers = array("Header 1", "Header 2", "Header 3");
$data = array(
    array("Data 1", "Data 2", "Data 3"),
    array("Data 4", "Data 5", "Data 6"),
    array("Data 7", "Data 8", "Data 9")
);

// Set table width and column count
$tableWidth = 190;
$colCount = count($headers);

// Set font and font size
$pdf->SetFont('Arial', '', 12);

// Print table headers
foreach($headers as $header) {
    $pdf->Cell($tableWidth/$colCount, 10, $header, 1, 0, 'C');
}
$pdf->Ln();

// Print table data
foreach($data as $row) {
    foreach($row as $cell) {
        $pdf->Cell($tableWidth/$colCount, 10, $cell, 1, 0, 'C');
    }
    $pdf->Ln();
}

// Output PDF
$pdf->Output();