簡體   English   中英

將客戶數據提取到CSV文件的所有列中

[英]Pull customer data into all columns in a csv file

這是我當前的代碼,該代碼將所有數據提取到單個列中。 我希望它可以將數據放入其自己的列中。 謝謝你的幫助

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

這是我在這里的第一篇文章,我發現這里的所有信息都非常有用。 謝謝你所做的一切。

編輯:這是語法錯誤的當前代碼

<?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);

我建議調查fputcsv。

PHP手冊

看起來您的代碼正在遍歷所有數據,並打印出email 然后再次循環並打印出first_name ...我還沒有測試過,但這使用了fputcsv來寫文件-我只更改了代碼的那些部分。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM