简体   繁体   中英

PHP DOMPDF display table in my db

I am new to DOMPDF and I am trying to print to pdf using dompdf all the records that is unarchive in my table. This is the code to print the html in pdf.

<?php

// include autoloader
require_once('../autoload.inc.php');

// Reference the dompdf namespace
use Dompdf\Dompdf;

$dompdf = new Dompdf();

include("../config.php");
$tbl_name = "patients";
$sql = "SELECT * FROM $tbl_name WHERE archive = 1";
$result = mysqli_query($connection, $sql);
$total = mysqli_num_rows($result);

$html = "

<style>
table {
    font-family: arial, sans-serif;
    border-collapse: collapse;
    width: 100%;
}

td, th {
    border: 1px solid #black;
    text-align: left;
}

</style>

<h4>
Total no. of preschooler '.$total.'
</h4>
<table>
  <tr>
    <th>ID</th>
    <th>Last Name</th>
    <th>First Name</th>
    <th>Gender</th>
    <th>Age</th>
    <th>Weight (kg)</th>
    <th>Height (cm)</th>
    <th>Nutritional Status</th>
    <th>Barangay</th>
  </tr>

";

while($row = mysqli_fetch_array($result))
{
    $html .= '
  <tr>
   <td>'.$row["p_id"].'</td>
   <td>'.$row["last_name"].'</td>
   <td>'.$row["first_name"].'</td>
   <td>'.$row["Gender"].'</td>
   <td>'.$row["last_name"].'</td>
   <td>'.$row["Years"].'</td>
   <td>'.$row["wt"].'</td>
   <td>'.$row["ht"].'</td>
   <td>'.$row["interp"].'</td>
   <td>$'.$row["barangay"].'</td>
  </tr>
 ';
}

$html .= '</table>';

$dompdf->loadHtml($html);
$dompdf->setPaper('A4', 'landscape');
$dompdf->render();

// Output
$dompdf->stream('coderworld',array('Attachment'=>0));

?>

I tried to check each spelling and syntax carefully but I think its correct. It do not display the rows but it displays the heading of the table.

Looking for help. Thanks in advance.

Try updating while($row = mysqli_fetch_array($result))

With while($row = mysqli_fetch_array($result, MYSQLI_ASSOC))

Hope it helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM