Solved

csv download issue

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now