tags:

views:

449

answers:

2

I have a function that exports a table into a CSV file, then I open that file using a spreadsheet application.

Is there a way to set the header of the CSV to name each column appropriately.

For example:

I have a table the contains first name, last name, email, and comments.

And the table is set as: fname, lname, email, comments

So the table exports as:

fname, lname, email, comments
john, doe, [email protected], I am John Doe this is my comment

I want to change the headers (fname, lname, email, comments) to something more readable, so it would be like this:

First Name, Last Name, Email, Comments
john, doe, [email protected], I am John Doe this is my comment

Here is the code I have:

function exportNamesCommentsCSV($table,$filename = 'volunteer_2009_comments.csv') {
    $csv_terminated = "\n";
    $csv_separator = ",";
    $csv_enclosed = '"';
    $csv_escaped = "\\";
    $sql_query = "select lname, fname, comments from volunteers_2009";

    // Gets the data from the database
    $result = mysql_query($sql_query);
    $fields_cnt = mysql_num_fields($result);

    $schema_insert = '';

    for ($i = 0; $i < $fields_cnt; $i++) {
     $l = $csv_enclosed . str_replace($csv_enclosed, $csv_escaped . $csv_enclosed,
      stripslashes(mysql_field_name($result, $i))) . $csv_enclosed;
     $schema_insert .= $l;
     $schema_insert .= $csv_separator;
    } // end for

    $out = trim(substr($schema_insert, 0, -1));
    $out .= $csv_terminated;

    // Format the data
    while ($row = mysql_fetch_array($result)) {
     $schema_insert = '';
     for ($j = 0; $j < $fields_cnt; $j++) {
      if ($row[$j] == '0' || $row[$j] != '') {

       if ($csv_enclosed == '') {
        $schema_insert .= $row[$j];
       } else {
        $schema_insert .= $csv_enclosed . 
        str_replace($csv_enclosed, $csv_escaped . $csv_enclosed, $row[$j]) . $csv_enclosed;
       }
      } else {
       $schema_insert .= '';
      }

      if ($j < $fields_cnt - 1) {
       $schema_insert .= $csv_separator;
      }
     } // end for

     $out .= $schema_insert;
     $out .= $csv_terminated;
    } // end while

    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Content-Length: " . strlen($out));
    header("Content-type: text/x-csv");
    header("Content-Disposition: attachment; filename=$filename");
    echo $out;
    exit;
}
+1  A: 

The header of a CSV file is just the first line. So you'd change this block:

for ($i = 0; $i < $fields_cnt; $i++) {
    $l = $csv_enclosed . str_replace($csv_enclosed, $csv_escaped . $csv_enclosed,
            stripslashes(mysql_field_name($result, $i))) . $csv_enclosed;
    $schema_insert .= $l;
    $schema_insert .= $csv_separator;
} // end for

$out = trim(substr($schema_insert, 0, -1));
$out .= $csv_terminated;

You could change it to something very simple, along the lines of:

$out .= "First Name, Last Name, Email, Comments\n";

unless I have completely misunderstood your question.

Edit: Your example code shows one specific query. If it needs to work for multiple queries, you'll have to find a source of those friendly names. For example, you could pass the names in as an argument or store them in the database.

derobert
+1  A: 

You could change the query to rename the field names as required.

$sql_query = "select lname, fname, comments from volunteers_2009";

Becomes

$sql_query = "select fname as 'First Name', lname as 'Last Name'," 
           . "Comments from volunteers_2009";
stukelly