Write custom column names to query result file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Joe Gazda

    #1

    Write custom column names to query result file

    I'm a relative newbie to PHP, but have been able to put together some PHP
    code to generate a CSV/XLS file from a Query result. Now, I would like to
    include custom column names instead of the MySQL column table names. I know
    that there are codes to generate tabs and carriage returns, but can't find
    anything about including "commas" in a string to output to the file to
    separate the custom field names. I'd appreciate some help with a line of
    code to insert those custom column names like:

    First Name | Last Name | DOB | etc.

    Thanks,

    Joe G.

    The generated file is set to the company name + date.xls and output the data
    correctly. My PHP code is:

    <?
    @session_start( );
    // Remove spaces from Company Name
    $trim_name = str_replace(" ", "", $co_name);
    $file_name=$tri m_name.date("md Y").".xls";
    echo $file_name;
    // Connect to Database
    include("dbinfo .inc.php");
    mysql_connect(l ocalhost,$usern ame,$pass);
    @mysql_select_d b($database) or die( "Unable to select database");
    // Find the Number of Fields
    $select = "SELECT * FROM employee WHERE co_name='$co_na me'";
    $export = mysql_query($se lect);
    $fields = mysql_num_field s($export);
    // Extract Database Fields
    for ($i = 0; $i < $fields; $i++) {
    $header .= mysql_field_nam e($export, $i) . "\t";
    }
    // Extract your Data
    while($row = mysql_fetch_row ($export)) {
    $line = '';
    foreach($row as $value) {
    if ((!isset($value )) OR ($value == "")) {
    $value = "\t";
    } else {
    $value = str_replace('"' , '""', $value);
    $value = '"' . $value . '"' . "\t";
    }
    $line .= $value;
    }
    $data .= trim($line)."\n ";
    }
    $data = str_replace("\r ","",$data) ;
    // Default Message
    if ($data == "") {
    $data = "\n(0) Records Found!\n";
    }
    $fp = fopen($file_nam e,"a"); // $fp is now the file pointer to file
    $file_name
    if($fp){
    $fp = fopen($file_nam e,"a");
    fwrite($fp,$dat a); // Write information to the file
    fclose($fp); // Close the file
    echo "File saved successfully";
    } else {
    echo "Error saving file!";
    echo "Use your Browser Back Button to Return";
    }
    ?>


  • NC

    #2
    Re: Write custom column names to query result file

    Joe Gazda wrote:[color=blue]
    >
    > I'm a relative newbie to PHP, but have been able to put together some[/color]
    PHP[color=blue]
    > code to generate a CSV/XLS file from a Query result. Now, I would[/color]
    like to[color=blue]
    > include custom column names instead of the MySQL column table names.[/color]
    I know[color=blue]
    > that there are codes to generate tabs and carriage returns, but can't[/color]
    find[color=blue]
    > anything about including "commas" in a string to output to the file[/color]
    to[color=blue]
    > separate the custom field names.[/color]

    Just replace "\t" with ",". That's it.
    [color=blue]
    > I'd appreciate some help with a line of code to insert those custom
    > column names like:
    >
    > First Name | Last Name | DOB | etc.[/color]

    OK, right now you have:
    [color=blue]
    > for ($i = 0; $i < $fields; $i++) {
    > $header .= mysql_field_nam e($export, $i) . "\t";
    > }[/color]

    Just replace it with

    $header = "First Name,Last Name,DOB,etc.";

    Cheers,
    NC

    Comment

    • Joe Gazda

      #3
      Re: Write custom column names to query result file

      Substituting the code yields the field name string in the first cell of MS
      Excel.
      I've just decided to have the client "import" the data fields into an
      existing Excel Spreadsheet with the Custom Field Names already in place.
      Thanks for you help, anyway.

      "NC" <nc@iname.com > wrote in message
      news:1115136464 .173562.219030@ z14g2000cwz.goo glegroups.com.. .[color=blue]
      > Joe Gazda wrote:[color=green]
      >>
      >> I'm a relative newbie to PHP, but have been able to put together some[/color]
      > PHP[color=green]
      >> code to generate a CSV/XLS file from a Query result. Now, I would[/color]
      > like to[color=green]
      >> include custom column names instead of the MySQL column table names.[/color]
      > I know[color=green]
      >> that there are codes to generate tabs and carriage returns, but can't[/color]
      > find[color=green]
      >> anything about including "commas" in a string to output to the file[/color]
      > to[color=green]
      >> separate the custom field names.[/color]
      >
      > Just replace "\t" with ",". That's it.
      >[color=green]
      >> I'd appreciate some help with a line of code to insert those custom
      >> column names like:
      >>
      >> First Name | Last Name | DOB | etc.[/color]
      >
      > OK, right now you have:
      >[color=green]
      >> for ($i = 0; $i < $fields; $i++) {
      >> $header .= mysql_field_nam e($export, $i) . "\t";
      >> }[/color]
      >
      > Just replace it with
      >
      > $header = "First Name,Last Name,DOB,etc.";
      >
      > Cheers,
      > NC
      >[/color]


      Comment

      Working...