Solved

csv download issue

Posted on 2006-10-30
4
220 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

687 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