TCPDF, SQL, HTML <table> integration and output question

15 Views Asked by At

So this code kinda works. The portion that doesn't work is the filling in of the table information (specifically the got here 2,3,4 do not show up on the generated PDF). I know the SQL works as I've tested is against the database and it gives the proper output. The $site_name is properly selected but everything I've tried ONLY gives me the header and the site name, nothing else.

<?php 

include('db_connection.php');

$selected_date = '2024-03-25';
    
ob_end_clean(); 
//require('lib/fpdf/fpdf.php'); 
require('lib/TCPDF/tcpdf.php');
  
// Instantiate and use the FPDF class  
$pdf = new tcpdf('L', 'mm', 'A4', true, 'UTF-8', false);

//Add a new page 
$pdf->AddPage(); 
  
// Set the font for the text 
$pdf->SetFont('Courier', '', 12); 
  
$sql = "SELECT l.name AS site_name, b.batt_bkup_notes AS batt_bkup_notes, b.bug_zpr_notes AS bug_zpr_notes, b.notes AS building_notes, ge.BH_notes AS BH_notes, ge.BATT_notes AS BATT_notes, ge.BATchrg_notes AS BATchrg_notes, ge.general_notes AS general_notes, g.road_notes AS road_notes, g.grounds_notes AS grounds_notes, g.guys_notes AS guy_notes, g.twr_gnd_notes AS twr_gnd_notes
        FROM locations l
        LEFT JOIN building b ON b.loc_id = l.loc_id AND DATE(b.logged) = '$selected_date'
        LEFT JOIN grounds g ON g.loc_id = l.loc_id AND DATE(g.logged) = '$selected_date'
        LEFT JOIN generator ge ON ge.loc_id = l.loc_id AND DATE(ge.logged) = '$selected_date'
        WHERE b.batt_bkup_notes IS NOT NULL
           OR b.bug_zpr_notes IS NOT NULL
           OR b.notes IS NOT NULL
           OR ge.BH_notes IS NOT NULL
           OR ge.BATT_notes IS NOT NULL
           OR ge.BATchrg_notes IS NOT NULL
           OR ge.general_notes IS NOT NULL
           OR g.road_notes IS NOT NULL
           OR g.grounds_notes IS NOT NULL
           OR g.guys_notes IS NOT NULL
           OR g.twr_gnd_notes IS NOT NULL
        order by l.name";        
        
$result = $conn->query($sql);

// Create the table header
$header = <<<EOD1
  <table border="1" cellpadding="4">
      <tr>
         <th>Site Name</th>
         <th>Building Notes</th>
         <th>Grounds Notes</th>
         <th>Generator Notes</th>
      </tr>
EOD1;

$pdf->WriteHTML($header, true, false, true, false, '');
        
  // Add table rows with site information and notes
  if ($result->num_rows > 0) {
      while ($row = $result->fetch_assoc()) {
          $site_name = $row['site_name'];
          
          $batt_bkup_notes = $row['batt_bkup_notes'];
          $bug_zpr_notes = $row['bug_zpr_notes'];
          $building_notes = $row['building_notes'];
          
          $road_notes = $row['road_notes'];
          $guy_notes = $row['guy_notes'];
          $twr_gnd_notes = $row['twr_gnd_notes'];
          $grounds_notes = $row['grounds_notes'];
          
          $BH_notes = $row['BH_notes'];
          $BATT_notes =  $row['BATT_notes'];
          $BATchrg_notes = $row['BATchrg_notes'];
          $general_notes = $row['general_notes'];
          
          $table_row = <<<EOD2
             <tr>
                <td> $site_name </td>
                <td>gothere2</td>
                <td>got here 3</td>
                <td>got here 4</td>
             </tr>
          EOD2;
          $pdf->WriteHTML($table_row, true, false, false, false, '');
       }
  }
 
// Close the table
$footer = '</table>';
$pdf->WriteHTML($footer, true, false, true, false, '');
  
// return the generated output 
$pdf->Output('I'); 
  
?>

I've tried using fpdf, I've tried modifying examples from both sets of documentations. As soon as I split the table rows from the header i run into the problem described.

I'd like it to populate the other columns with date from the database but I figure if I can get it to show me the "got here's" then I can easily but the variables I want in their place.

Been working on his for several days before reaching out. Thanks!

0

There are 0 best solutions below