1

I have developed conversion of SQL-SERVER Table into downloadable Excel using PHP. But that is for one specific table. I want to develop something which can take any number of columns.

In my current code i am defining in headers of html about my table header name. I do not want that ..means firstly I have developed for one specific table.. what if in stored procedure there are more than 2 tables so in code I can't define headers name for each table .If there is only table than It won't be a problem if there are more than 2 tables than It would be problem . so what shall i use instead of tr tag so that it can take any name and number of columns for converting it into excel ?

<?php
// Database Connection file
include('connection.php');
?>
<table border="1">
<thead>
   <tr>
      <th>ID</th>
      <th>LastName</th>
      <th>FirstName</th>
      <th>Address</th>
      <th>phone</th>
   </tr>
</thead>

<?php
// File name
$filename="EmpData";
// Fetching data from data base
$query = sqlsrv_query($conn, "select * from CVL_USERS1");
$cnt = 1;
while ($row = sqlsrv_fetch_array($query)) {
?>
<tr>
   <td><?php echo $cnt;  ?></td>
   <td><?php echo $row['ID'];?></td>
   <td><?php echo $row['LastName'];?></td>
   <td><?php echo $row['FirstName'];?></td>
   <td><?php echo $row['Address'];?></td>
   <td><?php echo $row['phone'];?></td>
</tr>

<?php 
$cnt++;
// Genrating Execel  filess
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=".$filename."-Report.xls");
header("Pragma: no-cache");
header("Expires: 0");
}?>

As you can see, in my code that I have defined header's name .. I do not want that so what shall i use in place of <th>ID</th>.

Zhorov
  • 28,486
  • 6
  • 27
  • 52
  • Try to use `sqlsrv_field_metadata()`. A similar [Q&A](https://stackoverflow.com/questions/51621902/large-html-from-sql-server-using-php/51626866#51626866). – Zhorov Sep 01 '22 at 07:42

0 Answers0