Solved

csv download issue

Posted on 2006-10-30
4
196 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

785 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