title: Exporting a dynamic table to Excel in PHP
body: <p>I have a large table with an unknown amount of columns that I want to be able to export to MS Excel via a download link. I have done this before with no issues. However, that was when I had a known number or columns. I currently am able to successfully send data to an Excel file. The table headers display correctly across the top of the table. The problem is with the data. Instead of placing the data across the page in the correct columns, it is putting it all in the first column. Here is part of my code:</p>
  
  <pre><code>while($row = mysql_fetch_row($result))          
  {
      // Loops through the results
      for($i=0; $i &lt; $num_cols; $i++)
      {
          // Prints out the data in a table cell
          echo("&lt;Td class='data'&gt;$row[$i]&lt;/Td&gt;");
          $FileRecord = Array($row[$i]);
          $exportFile-&gt;Export_Record($FileRecord);
      }
  }
  </code></pre>
  
  <p>Normally I do <code>$FileRecord = Array($row[0], $row[1], ..., $row[n])</code> and everything works great, but I am not sure how to do that if I don't know how many columns there are. Any help would be great!</p>
  
  <p>I am not using any library. <code>$exportFile</code> is from a function that I am using. It looks like this:  </p>
  
  <pre><code>class CSV_File {
  private $out='';
  private $name='';
  private $column_names="";
  private $count = 0;
  
  //Creates the file name and the header columns
  function __Construct($buf, $names) {
      $GLOBALS["name"] = $buf;
      $GLOBALS["column_names"] = ($names."\n");
  }
  
  public function Export_Record($array) {
      if (!is_array($array))
          throw new Exception("NOT AN ARRAY");
      for ($i = 0; $i &lt;= count($array); $i++) {
          $GLOBALS["out"] .= $array[$i];
  
          if ($i &lt; count($array)-1) {
              $GLOBALS["out"] .= ",";
          }
      }
      $GLOBALS["out"] .= "\n";
      $GLOBALS["out"]++;
  }
  
  public function ExportButton($align) {
          $output = $GLOBALS["out"];
          $filename = $GLOBALS["name"];
          $columns = $GLOBALS["column_names"];
          if ($align == null)
              $align = "align";
          echo("  &lt;$align&gt;&lt;form name=\"export\" action=\"export.php\" method=\"post\"&gt;
                  &lt;button type=\"submit\" style='border: 0; background: transparent; font-size: 12px;font-weight:bold;'&gt;
                  &lt;img src = 'images/icon_csv.png' width = '16' height ='16' alt ='Download Data'&gt; Download&lt;/button&gt;
                  &lt;input type=\"hidden\" value=\"$columns\" name=\"csv_hdr\"&gt;
                  &lt;input type=\"hidden\" value=\"$filename\" name=\"fileprefix\"&gt;
                  &lt;input type=\"hidden\" value=\"$output\" name=\"csv_output\"&gt;
                  &lt;/form&gt;&lt;/align&gt;");
  }
  }
  </code></pre>
  
  <p>And then export.php looks like this: </p>
  
  <pre><code>if (isset($_POST['csv_hdr'])) 
  {
  $out .= $_POST['csv_hdr'];
  $out .= "\n";
  } 
  
  if (isset($_POST['csv_output'])) 
  {
  $out .= $_POST['csv_output'];
  }
  
  if (isset($_POST['fileprefix'])) 
  {
  $fname .= $_POST['fileprefix'];
  }
  
  //Now we're ready to create a file. This method generates a filename based on the current date &amp; time.
  $filename = $fname."_".date("Y-m-d_H-i",time());
  
  //Generate the CSV file header
  header("Content-type: application/vnd.ms-excel");
  header("Content-disposition: csv" . date("Y-m-d") . ".csv");
  header("Content-disposition: filename=".$filename.".csv");
  
  //Print the contents of out to the generated file.
  print $out;
  
  //Exit the script
  exit;' 
  </code></pre>