Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 158625
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T10:42:53+00:00 2026-05-11T10:42:53+00:00

I have a function that exports a table into a CSV file, then I

  • 0

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, johndoe@email.com, 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, johndoe@email.com, 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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T10:42:53+00:00Added an answer on May 11, 2026 at 10:42 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 97k
  • Answers 97k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Sometimes the process of decreasing the learning rate over time… May 11, 2026 at 7:22 pm
  • Editorial Team
    Editorial Team added an answer At the bottom of the MSDN page there's this comment:… May 11, 2026 at 7:22 pm
  • Editorial Team
    Editorial Team added an answer You can add the facebook swc to your ide by… May 11, 2026 at 7:22 pm

Related Questions

I have a function that automatically exports a table into a CSV file, then
I have a table (volunteers_2009) that has all the volunteers stored within, then I
In our application we have style sheets to define common colors etc… I wrote
I have a function that exports a table to CSV and in the query

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.