TCPDF - Data not displaying correctly in table columns

76 Views Asked by At

I am working on generating a PDF using TCPDF in PHP, where I fetch data from two MySQL tables, checklist and checklist_notez. The goal is to display the data from these tables in separate columns in a PDF table.

The issue I'm facing is that, while the data from the checklist_notez table is fetched correctly, it is not displaying in the expected column (Checklist_notez) in the PDF table. Instead, all data is being displayed in the Checklist column.

I have tried various approaches, including using mysqli_fetch_assoc and mysqli_fetch_row for fetching data, and attempted to organize it into separate columns. However, the problem persists.enter image description here

Here is my code:

<?php
require_once('tcpdf.php');
include('config.php');

if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['reg_number'])) {
    $reg_number = mysqli_real_escape_string($link, $_POST['reg_number']);

    // Fetch data from checklist
    $checklistSql = "SELECT * FROM checklist WHERE reg_number = '$reg_number'";
    $checklistResult = mysqli_query($link, $checklistSql);

    if (!$checklistResult) {
        die("Error fetching checklist data: " . mysqli_error($link));
    }

    // Fetch data from checklist_notez
    $noteSql = "SELECT * FROM checklist_notez WHERE reg_number2 = '$reg_number'";
    $noteResult = mysqli_query($link, $noteSql);

    if (!$noteResult) {
        die("Error fetching checklist_notez data: " . mysqli_error($link));
    }

    // Create PDF instance
    $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
    $pdf->AddPage();
    $pdf->SetFont('times', 12);

    // Display headers
    $pdf->Cell(70, 10, 'Checklist', 1);
    $pdf->Cell(70, 10, 'Checklist_notez', 1);
    $pdf->Ln();

    // Display data from checklist under "Checklist" column
    while ($checklistRow = mysqli_fetch_assoc($checklistResult)) {
        foreach ($checklistRow as $key => $value) {
            if ($key !== 'reg_number2') {
                $pdf->Cell(70, 10, $key . ': ' . $value, 1);
                $pdf->Ln(); // Add a line break after each key-value pair
            }
        }
    }

    // Add an empty cell for separation
    $pdf->Cell(70, 10, '', 1);

    // Display data from checklist_notez under "Checklist_notez" column
    while ($noteRow = mysqli_fetch_assoc($noteResult)) {
        foreach ($noteRow as $key => $value) {
            if ($key !== 'reg_number2') {
                $pdf->Cell(70, 10, $key . ': ' . $value, 1);
                $pdf->Ln(); // Add a line break after each key-value pair
            }
        }
    }

    // Close the result sets
    mysqli_free_result($checklistResult);
    mysqli_free_result($noteResult);

    // Close the database connection
    mysqli_close($link);

    // Output the PDF as a download
    $pdfFileName = 'combined_checklist_' . $reg_number . '.pdf';
    $pdf->Output($pdfFileName, 'D'); // 'D' to force download
}
?>

Also, there is only one value that is showing in the correct column, and that is "service_book2". So all values with ends with a suffix "2" should be in the checklist_notez table. I would appreciate any insights into what might be causing this issue and how I can ensure that the data from the checklist_notez table appears in the correct column in my PDF table.

enter image description here

I attempted to generate a PDF table using PHP and TCPDF, fetching data from two MySQL tables (checklist and checklist_notez). The goal was to display the data from each table in separate columns within the PDF table.

What I expected:

I expected the PDF to have two distinct columns labeled "Checklist" and "Checklist_notez" at the top. Each key-value pair from the respective database tables should populate their designated columns.

What actually resulted:

The issue is that, despite successfully fetching and looping through data from both tables, only the data from the checklist table is being displayed in the "Checklist" column. The data from the checklist_notez table is not appearing in the "Checklist_notez" column as expected.

I've ensured that the data in both database tables is accurate, and the PDF is being generated without errors. However, the problem lies in how the data is presented within the PDF table.

0

There are 0 best solutions below