简体   繁体   中英

Pull customer data into all columns in a csv file

This is my current code which pulls all data into a single column. I would like it to pull data into its own column. Thanks for any help

<?php
include('codelibrary/inc/variables.php');
include("session.php");
$obj= new database_class();

$query = $obj->getAnyTableAllData($obj->getTable("var_customer")," and email != '' order by email ");
$filename = 'customer_email.csv';
$fp = fopen($filename, 'w');

foreach ($query as $k=>$v) {
    $_csv_data= $v["email"]. "\n";
    fwrite( $fp, $_csv_data );
}


foreach ($query as $k=>$v) {
    $_csv_data= $v["first_name"]. "\n";
    fwrite( $fp, $_csv_data );
}


foreach ($query as $k=>$v) {
    $_csv_data= $v["business"]. "\n";
    fwrite( $fp, $_csv_data );
}


foreach ($query as $k=>$v) {
    $_csv_data= $v["phone_no"]. "\n";
    fwrite( $fp, $_csv_data );
}


foreach ($query as $k=>$v) {
    $_csv_data= $v["shipping_address"]. "\n";
    fwrite( $fp, $_csv_data );
}

fclose($fp);

if(is_file($filename))
{
    $size=filesize("$filename");
    header("Content-type: application/csv");
    header("Content-Length: $size");
    header("Content-Disposition: attachment; filename=$filename");
    header("Pragma: no-cache");
    header("Expires: 0");
    readfile($filename);
    exit;
 }
else
{
    echo "Invalid file!";
}
?>

This is my first post on here and I've found all the info here very informative. Thanks for all you do.

EDIT: Here is the current code with the syntax error

<?php
include('codelibrary/inc/variables.php');
include("session.php");
$obj= new database_class();

$query = $obj->getAnyTableAllData($obj->getTable("var_customer")," and email != '' order by email ");
$filename = 'customer_email.csv';
$fp = fopen($filename, 'w');


# let's get keys into an order that you like
$fields = array('email','first_name','business','phone_no','shipping_address');

foreach ($query as $k=>$v) {
// put the fields for this row into an array
foreach ($fields as $field) { 
    $data[] = $v[$field];
}
fputcsv($fp, $data);
unset($data);
}

 fclose($fp);

if(is_file($filename))
{
$size=filesize("$filename");
header("Content-type: application/csv");
header("Content-Length: $size");
header("Content-Disposition: attachment; filename=$filename");
header("Pragma: no-cache");
header("Expires: 0");
readfile($filename);

 exit;
 }
 else
  {
        echo "Invalid file!";
         }
?>

对于CSV,可以使用fputcsv而不是fwrite ,只需将其传递给要插入行的列数组即可。

$array = array('column one contents', 'column two contents'); fputcsv($fp, $array);

I would suggest looking into fputcsv.

php manual

It looks like your code was looping through all the data, printing out the email . Then looping again and printing out the first_name ... I haven't tested it, but this uses fputcsv to write the file - I changed only those parts of the code.

<?php
include('codelibrary/inc/variables.php');
include("session.php");
$obj= new database_class();

    $query = $obj->getAnyTableAllData($obj->getTable("var_customer")," and email != '' order by email ");
$filename = 'customer_email.csv';
$fp = fopen($filename, 'w');


# let's get keys into an order that you like
$fields = array('email','first_name','business','phone_no','shipping_address');

foreach ($query as $k=>$v) {
    // put the fields for this row into an array
    foreach ($fields as $field) { 
        $data[] = $v[$field];
    }
    $lines[] = $data; // add the row of data to $lines
    unset($data);
}

fputcsv($fp, $lines);

fclose($fp);

if(is_file($filename))
{
    $size=filesize("$filename");
    header("Content-type: application/csv");
    header("Content-Length: $size");
    header("Content-Disposition: attachment; filename=$filename");
    header("Pragma: no-cache");
    header("Expires: 0");
    readfile($filename);

     exit;
     }
 else
      {
            echo "Invalid file!";
             }
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM