0

I had created a table where there will be leave details of employees and applied sorting of the leave details from date to date. It will display me in table format. Now i want to add button like Export to Excel and Export to pdf so that i can download the table from there in excel or pdf format.

Below is code

<form name="filter" method="POST">

                            <input type="date" name="start">
                            <input type="date" name="end">
                            <input type="submit" name="submit" value="Filter">
                            </form>

   <table id="example" class="display" style="width:100%">
<tbody>
<?php
  if(isset($_POST['submit'])){
    $Status=1;
    $end = $_POST['end'];
    $start = $_POST['start'];
    $sql = "SELECT * FROM tblleaves 
       WHERE Status='".$Status."' AND (start BETWEEN '".$start."' AND '".$end."' OR end BETWEEN '".$start."' AND '".$end."' + INTERVAL 1 DAY )" ; 
   $result = mysqli_query($conn,$sql)or die(mysqli_error($conn));

     echo "<table>";
     echo "<thead>
                                    <tr>

                                         <th>title</th>
                                         <th>empid</th>
                                         <th>Team</th>
                                         <th>Leave Type</th>
                                        <th>Start Date</th>
                                        <th>End Date</th>
                                         <th>Description</th>

                                    </tr>
                                </thead>";

  while($row = mysqli_fetch_array($result)) {
$id = $row['id'];
$LeaveType = $row['LeaveType'];
$end = $row['end'];
$start=$row['start'];
$Description=$row['Description'];

$Status=$row['Status'];
$empid=$row['empid'];
$team=$row['team'];
$title=$row['title'];
echo "<td style='width: 200px;'>".$title."</td><td>".$empid."</td><td>".$team."</td><td style='width: 100px;'>".$LeaveType."</td><td>".$start."</td><td>".$end."</td><td>".$Description."</td></tr>";
      } 

    echo "</table>";

}
  else{

require('../includes/config.php');

$sql = "SELECT * FROM tblleaves where Status=1 order by id desc";

$result = mysqli_query($conn,$sql)or die(mysqli_error());

 echo "<table>";
 echo "<thead>
                                    <tr>

                                        <th>title</th>
                                         <th>empid</th>
                                         <th>Team</th>
                                        <th>Leave Type</th>
                                        <th>Start Date</th>
                                        <th>End Date</th>
                                         <th>Description</th>


                                    </tr>
                                </thead>";

  while($row = mysqli_fetch_array($result)) {
$id = $row['id'];
$LeaveType = $row['LeaveType'];
$end = $row['end'];
$start=$row['start'];
$Description=$row['Description'];

$Status=$row['Status'];
$empid=$row['empid'];
$team=$row['team'];
$title=$row['title'];
echo "<td style='width: 200px;'>".$title."</td><td>".$empid."</td><td>".$team."</td><td style='width: 100px;'>".$LeaveType."</td><td>".$start."</td><td>".$end."</td><td>".$Description."</td></tr>";
} 

  echo "</table>";
  mysqli_close($conn);

    }
 ?>                     </tbody>
                            </table>

2 Answers2

0

To export html table into pdf you can used dompdf library.

Ravi Thanki
  • 251
  • 2
  • 14
0

There are multiple PDF libraries available to convert table to pdf, like DOMPDF, FPDF, etc. Also you can find libraries to convert table to .xls

Please go through this once. Might be you find this useful. How to export an html table as an xlsx file

Vikash Mishra
  • 349
  • 3
  • 18