How to Print your Data in Table

This simple tutorial helps you on how to Print your Data in Table. This simple function one of the most needed in one system. If you are looking for this kind of tutorial or function then you are at the right place. Most of the systems have a print function for their report or inventory to print all the report. In this part, we are going to teach you on how to print your data in a table. We are going to prepare the database then we display it to the table on the web page and construct the button link for the next page contains all data from the database. Study the source code below. This is our button link for the next page.
  1. <form method="post" action="data-print.php" target="_new" class="form-inline">
  2. <div class="form-group">
  3. <label for="Date_From">Click to Print</label>
  4. <button type="submit" name="search" class="btn btn-primary">Print Data</button>
  5. </div>
  6. </form>
This is our data in a table.
  1. <tr>
  2. <th>Member Name</th>
  3. <th>Contact</th>
  4. <th>Date Added</th>
  5. </tr>
  6. <?php
  7. include ('database.php');
  8. $result = $database->prepare ("SELECT * FROM tbl_member order by tbl_member_id DESC");
  9. $result ->execute();
  10. for ($count=0; $row_member = $result ->fetch(); $count++){
  11. $id = $row_member['tbl_member_id'];
  12. ?>
  13. <tr>
  14. <td><?php echo $row_member['tbl_member_name']; ?></td>
  15. <td><?php echo $row_member['tbl_member_contact']; ?></td>
  16. <td><?php echo date("M d, Y h:i:s A", strtotime ($row_member['tbl_member_added'])); ?></td>
  17. </tr>
  18. <?php } ?>
For the page to print save this file and named it as "data-print.php". First, we need a simple script for the print function. All you need to do is copy this script and paste it to the HEAD tag of your web page.
  1. <script>
  2. function printPage() {
  3. window.print();
  4. }
  5. </script>
We are going to construct a button to run the script above to execute the Print Function after the user clicks the button. Here's the source code below.
  1. <button type="submit" id="print" onclick="printPage()">Print</button>
Lastly, this is the data where the users print it after clicking the button. Take a look the source code it's all the same when we displaying all data from the database.
  1. <?php
  2. include ('database.php');
  3. $result = $database->prepare ("SELECT * FROM tbl_member order by tbl_member_id DESC");
  4. ?>
  5. <tr>
  6. <th>Member Name</th>
  7. <th>Contact</th>
  8. <th>Date Added</th>
  9. </tr>
  10. </thead>
  11. <?php
  12. $result ->execute();
  13. for ($count=0; $row_member = $result ->fetch(); $count++){
  14. $id = $row_member['tbl_member_id'];
  15. ?>
  16. <tr>
  17. <td style="text-align:center;"><?php echo $row_member['tbl_member_name']; ?></td>
  18. <td style="text-align:center;"><?php echo $row_member['tbl_member_contact']; ?></td>
  19. <td style="text-align:center;"><?php echo date("M d, Y h:i:s A", strtotime ($row_member['tbl_member_added'])); ?></td>
  20. </tr>
  21. <tr>
  22. <td></td>
  23. <td></td>
  24. <td></td>
  25. </tr>
  26. <?php
  27. }
  28. ?>
  29. </tbody>

Output

This is our "index.php" displaying all the data from the database. Result This "data-print.php" file contains print button and data for ready to print. Result This would be the result after the users click the print button. Result That's all, I created this simple tutorial for beginners who really want to learn in a simple way of coding in PHP/MySQL. Hope you look find useful. Thank you.

Add new comment