Solved

csv download issue

Posted on 2006-10-30
4
205 Views
Last Modified: 2006-12-05
I am have written the following script to download results from my mysql query to csv. The code

define(db_host, "localhost");
define(db_user, "username");
define(db_pass, "passwd");
define(db_link, mysql_connect(db_host,db_user,db_pass));
define(db_name, "db_name");
mysql_select_db(db_name);

$select = "SELECT m.mail_email,t.mail_type FROM mailing AS m, mail_type AS t WHERE m.mail_type=t.type_id ORDER BY m.mail_type ASC";                
$export = mysql_query($select);
$fields = mysql_num_fields($export);

for ($j = 0; $j < $fields; $j++) {
    $header .= mysql_field_name($export, $j) . "\t";
}
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);

if ($data == "") {
    $data = "\n(0) Records Found!\n";                        
}

header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=emails.xls");
header("Pragma: no-cache");
header("Expires: 0");
print "$header\n$data";

This code dumps out the results which is perfect but now I want the results to start from the second row and the first row to have prefilled column name --- Email Address (column 1), Type (column 2). I will really appreciate it someone can tweak my code to allow for this functionality. Thanks

Sulen
0
Comment
Question by:sulentho
4 Comments
 
LVL 35

Accepted Solution

by:
Raynard7 earned 400 total points
ID: 17838000
Hi,

Firstly, Isnt the $header populating with your field names? if not I'd just change the field names in the select statement - ie

m.mail_email as 'Email Address', t.mail_type as 'Type'

then if you want to omit the first row you could just do

replace

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";
}


with

$cnt = 0;
while($row = mysql_fetch_row($export)) {
    if ($cnt > 0) {
        $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";
    } // end if
    $cnt++;
}
0
 

Author Comment

by:sulentho
ID: 17838300
Thank you sir, worked great.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
error while installing php56 in redhat enterprise linux 20 50
MVC - procedural PHP 10 35
Undefined variable problem 5 22
Php logic to add to date card 9 19
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question