0

How to print a table on while loop to get data from database using Dompdf?

Below are my codes that I have done but with fail result.

$html =  '<table>
<tr>
  <td>Date</td><td>Name</td>
</tr>';

// Query from mysql 
if (mysqli_num_rows($result) > 0) {
  while ($row = mysqli_fetch_assoc($result)) {
   $date = $row['date '];
   $name = $row['name'];

   $html . = '<tr>
    <td> ' . $date . ' </td>' . $name . '</td>
   </tr>';
  }
}

$html .= '</table>';

require('../dompdf/autoload.inc.php');
use Dompdf\Dompdf;
$dompdf = new Dompdf();
$dompdf->load_html($html);

$dompdf->render();
$dompdf->stream("Result.pdf",array("Attachment"=>0));
$dompdf->clear();
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Amran
  • 627
  • 3
  • 11
  • 30
  • now you went and edited without marking it as an edit, stating that you made a mistake. – Funk Forty Niner Nov 03 '16 at 02:31
  • sorry @Fred-ii- but how do mark it as an edit? I did mention on the update that I'am making a correction on the codes – Amran Nov 03 '16 at 02:44
  • 1
    *"Stack tip"*: Whenever you make a mistake, you need to specify in your (edited) question that you changed your code to what should have been the proper syntax. This is mostly because, when people post answers based on what you (originally) posted, others will look at the answer(s) with the proper syntax and compare it with what you now have, and may tell themselves: *"They have the right syntax, so why the answer(s)?"* And they might downvote the answer(s) because of this. So, always put your "new" code below your original question, with **"Edit:**, and a quick explanation ;-) – Funk Forty Niner Nov 03 '16 at 11:18

1 Answers1

2

1 The string must be quoted. Change

$html .= </table>

to

$html .= '</table>';

2 The operator ".=" must not have a space in it. Change

$html . = '<tr>

to

$html .= '<tr>

3 Finally, there is probably not a space in the "date", so Change

$date = $row['date '];

to

$date = $row['date'];
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
WEBjuju
  • 5,797
  • 4
  • 27
  • 36
  • thanks for the help @Webjuju. After I made the corrections, I have this error `Fatal error: Maximum execution time of 30 seconds exceeded` – Amran Nov 03 '16 at 02:45
  • just before your **require()** call, can you **die($html);** to be sure that the html portion is not the culprit. how many rows do you have? – WEBjuju Nov 03 '16 at 02:47
  • If I add the `die($html);`, the result works great. I have around 400 rows. – Amran Nov 03 '16 at 02:51
  • well, put a **die(__LINE__)** after each line until you find the issue. may i also recommend http://stackoverflow.com/questions/18050071/php-parse-syntax-errors-and-how-to-solve-them – WEBjuju Nov 03 '16 at 02:53
  • Thanks for your help. I found that the solution is I need to extend the `max_execution_time`. Just want to know if there is any issue if I extend the time. Maybe like performance or that is not the good solution? – Amran Nov 03 '16 at 03:23
  • sometimes these things just take time based on the power of the server generating the pdf. as they say, *"mileage may vary"* – WEBjuju Nov 03 '16 at 03:24